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:

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).
  • The public Python facade lives in poptrie/__init__.py and poptrie/ip_searcher.py.

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.4.2-cp38-abi3-win_amd64.whl (155.3 kB view details)

Uploaded CPython 3.8+Windows x86-64

poptrie-0.4.2-cp38-abi3-manylinux_2_34_x86_64.whl (252.2 kB view details)

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

poptrie-0.4.2-cp38-abi3-macosx_11_0_universal2.whl (212.1 kB view details)

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

File details

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

File metadata

  • Download URL: poptrie-0.4.2-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 155.3 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.4.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 fbb741af7530363aec1043dcf89a0cc2f9967e052ec1f33b5f27a5f21478e862
MD5 ac3efbd3a65110e5d98cde94c6d07d34
BLAKE2b-256 56c4eee62b888010df9565685848d4e6bf10e2640dad5b49caaef1a2fcb2724c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for poptrie-0.4.2-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 96953c44ed3f9ab4222ca2a97bd664168e3404fd2f3810a87233e5b3ed9c325c
MD5 7925ec275d54f33797e98890964571d1
BLAKE2b-256 d1ac75b2f2bfef982186e9c68ce2832f5f64fc1329c1198dca7bae90b7f46b8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for poptrie-0.4.2-cp38-abi3-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 8b33cefed473c2f46d54928a9e0de0cd2cc55a0c11d607c0c4baa4a7b3c25786
MD5 cd322495e4c09acee89ffdd99cbf8cf0
BLAKE2b-256 5e680e823ade7f09c813519e080dba17cb4e9379a77abce58a051ccc45c9320e

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