|
| 1 | +# poptrie |
| 2 | + |
| 3 | +Fast IP membership lookup backed by a Rust implementation and exposed to Python via PyO3. |
| 4 | + |
| 5 | +Chinese version: [`README-CN.md`](./README-CN.md). |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- Python 3.8+ |
| 10 | +- Rust toolchain |
| 11 | +- `maturin` |
| 12 | + |
| 13 | +## Build and Install |
| 14 | + |
| 15 | +Create a virtual environment and install the module in dev mode: |
| 16 | + |
| 17 | +```bash |
| 18 | +python -m venv .venv |
| 19 | +source .venv/bin/activate |
| 20 | +python -m pip install --upgrade pip maturin |
| 21 | +maturin develop --release |
| 22 | +``` |
| 23 | + |
| 24 | +Build wheels: |
| 25 | + |
| 26 | +```bash |
| 27 | +maturin build --release --out dist |
| 28 | +``` |
| 29 | + |
| 30 | +## Build the Binary Data |
| 31 | + |
| 32 | +The searcher reads a binary file produced by `build_bin.py`. |
| 33 | + |
| 34 | +```bash |
| 35 | +python build_bin.py |
| 36 | +``` |
| 37 | + |
| 38 | +If you change the builder logic, rebuild the bin file to match the 72-byte node alignment. |
| 39 | + |
| 40 | +## Python Usage |
| 41 | + |
| 42 | +```python |
| 43 | +import socket |
| 44 | +from pathlib import Path |
| 45 | + |
| 46 | +import poptrie |
| 47 | + |
| 48 | + |
| 49 | +bin_path = Path("china_ip.bin") |
| 50 | +searcher = poptrie.IpSearcher(str(bin_path)) |
| 51 | + |
| 52 | +ip_bytes = socket.inet_pton(socket.AF_INET, "1.0.1.1") |
| 53 | +print(searcher.is_china_ip(ip_bytes)) |
| 54 | + |
| 55 | +ips = ["1.0.1.1", "8.8.8.8", "240e::1", "2001:db8::"] |
| 56 | +print(searcher.batch_check_strings(ips)) |
| 57 | + |
| 58 | +v4_ips = ["1.0.1.1", "8.8.8.8", "110.16.0.1", "127.0.0.1"] |
| 59 | +packed_v4 = b"".join(socket.inet_pton(socket.AF_INET, ip) for ip in v4_ips) |
| 60 | +print(searcher.batch_check_packed(packed_v4, is_v6=False)) |
| 61 | +``` |
| 62 | + |
| 63 | +## Python Helper Class |
| 64 | + |
| 65 | +If you want a lightweight Python-facing wrapper with docstrings, you can use this class: |
| 66 | + |
| 67 | +```python |
| 68 | +from __future__ import annotations |
| 69 | + |
| 70 | +from pathlib import Path |
| 71 | +from typing import Iterable |
| 72 | + |
| 73 | +import poptrie |
| 74 | + |
| 75 | + |
| 76 | +class IpSearcher: |
| 77 | + """Poptrie IP lookup wrapper backed by Rust. |
| 78 | +
|
| 79 | + :param bin_path: Path to the binary data file. |
| 80 | + """ |
| 81 | + |
| 82 | + def __init__(self, bin_path: str | Path) -> None: |
| 83 | + self._searcher = poptrie.IpSearcher(str(bin_path)) |
| 84 | + |
| 85 | + def is_china_ip(self, ip_bytes: bytes) -> bool: |
| 86 | + """Check a single IPv4/IPv6 address in packed bytes. |
| 87 | +
|
| 88 | + :param ip_bytes: 4-byte IPv4 or 16-byte IPv6. |
| 89 | + :return: True if matched, otherwise False. |
| 90 | + """ |
| 91 | + return self._searcher.is_china_ip(ip_bytes) |
| 92 | + |
| 93 | + def batch_check_strings(self, ips: Iterable[str]) -> list[bool]: |
| 94 | + """Check a list of IP strings, preserving input order. |
| 95 | +
|
| 96 | + :param ips: IP strings (IPv4 or IPv6). |
| 97 | + :return: Match results aligned to input order. |
| 98 | + """ |
| 99 | + return self._searcher.batch_check_strings(list(ips)) |
| 100 | + |
| 101 | + def batch_check_packed(self, packed_ips: bytes, is_v6: bool) -> list[bool]: |
| 102 | + """Check a packed byte buffer containing IPv4 or IPv6 addresses. |
| 103 | +
|
| 104 | + :param packed_ips: Flat buffer of IP bytes. |
| 105 | + :param is_v6: True when each IP is 16 bytes, False for 4 bytes. |
| 106 | + :return: Match results aligned to the packed order. |
| 107 | + """ |
| 108 | + return self._searcher.batch_check_packed(packed_ips, is_v6=is_v6) |
| 109 | +``` |
| 110 | + |
| 111 | +## API Notes |
| 112 | + |
| 113 | +- `is_china_ip` accepts IPv4/IPv6 bytes (`len == 4` or `len == 16`). |
| 114 | +- `batch_check_strings` keeps input order and parses strings in Rust. |
| 115 | +- `batch_check_packed` expects a flat byte buffer with a fixed stride of 4 or 16. |
| 116 | + |
| 117 | +## Tests |
| 118 | + |
| 119 | +```bash |
| 120 | +python -m unittest discover tests |
| 121 | +``` |
| 122 | + |
| 123 | +## Example |
| 124 | + |
| 125 | +Run the included example: |
| 126 | + |
| 127 | +```bash |
| 128 | +python example.py |
| 129 | +python example_production.py |
| 130 | +``` |
0 commit comments