Skip to main content

High-performance IP and country lookup backed by Rust poptrie

Project description

poptrie

High-performance IP lookup backed by Rust. This public repository owns the final Python facade and publishes wheels.

Stable public contract:

  • from poptrie import IpSearcher
  • IpSearcher resolves to poptrie.ip_searcher.IpSearcher
  • native extension details are intentionally hidden behind the facade

Installation

Install from PyPI:

pip install poptrie

Usage

import socket
from pathlib import Path

from poptrie import IpSearcher


bin_path = Path("china-ip.bin")
searcher = IpSearcher(bin_path)

print("1.0.1.1" in searcher)
print(searcher.contains_ip("1.0.1.1"))
print(searcher.lookup_country("1.0.1.1"))
print(searcher.is_china("1.0.1.1"))

ips = ["1.0.1.1", "8.8.8.8", "240e::1", "2001:db8::"]
print(searcher.contains_ips(ips))
print(searcher.lookup_countries(ips))
print(searcher.matches_countries(ips, "CN"))

v4_ips = ["1.0.1.1", "8.8.8.8", "110.16.0.1", "127.0.0.1"]
packed_v4 = b"".join(socket.inet_pton(socket.AF_INET, ip) for ip in v4_ips)
print(searcher.contains_packed(packed_v4, is_v6=False))
print(searcher.lookup_countries_packed(packed_v4, is_v6=False))
print(searcher.matches_country_packed(packed_v4, "CN", is_v6=False))

Example

python example.py

Tests

Public facade verification:

PYTHONPATH=<private-src> python -m unittest discover tests

Notes

  • Country codes are returned as u16 in Rust and converted to 2-letter strings in Python.
  • *_packed methods are the high-throughput APIs for packed inputs (stride 4/16).
  • Wheel repack injects the public __init__.py and ip_searcher.py into the final package.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

poptrie-0.3.0-cp38-abi3-win_amd64.whl (151.6 kB view details)

Uploaded CPython 3.8+Windows x86-64

poptrie-0.3.0-cp38-abi3-manylinux_2_34_x86_64.whl (252.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ x86-64

poptrie-0.3.0-cp38-abi3-macosx_11_0_universal2.whl (212.4 kB view details)

Uploaded CPython 3.8+macOS 11.0+ universal2 (ARM64, x86-64)

File details

Details for the file poptrie-0.3.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: poptrie-0.3.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 151.6 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for poptrie-0.3.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6ba7d6b8f18e2dd5bfb3a21b6fd79c41092115edf581aa843bca0616fa3a2711
MD5 1722b371af6c4952f35229e8a36e14c6
BLAKE2b-256 453675494ed747eda9638ea1630133beebe8ff8e22d0401b9fbee9d1b7504434

See more details on using hashes here.

File details

Details for the file poptrie-0.3.0-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for poptrie-0.3.0-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 9a44a514ea8337c52862924bfad8a414534f4d8480d4b272bd750e23b75f447e
MD5 d3d3ab30995ccb362d483cfcaaf9023a
BLAKE2b-256 e3430598b4b5aef0a0358b3955f08695d7cbc84e69a3458da807cc776731386f

See more details on using hashes here.

File details

Details for the file poptrie-0.3.0-cp38-abi3-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for poptrie-0.3.0-cp38-abi3-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 d2099afa33949326edcce015f0f06966c7461475bb4027cc70c67423577f808f
MD5 f6442ba7105ff06dfb4ec06d9f125e54
BLAKE2b-256 9b6e4777cbc990fec6aeae4b269715a26c4124a71496aa80e57ccea47b8638eb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page