Skip to main content

Routing table-style IP prefix lookups

Project description

routelookup

A simple, pure Python implementation of routing table-style IP prefix lookups. Compatible with Python 3.10 and higher.

Features

  • Portable
  • No dependencies outside the standard library
  • Longest-prefix lookup is O(1) on average
  • routelookup.pytricia is a drop-in replacement for Pytricia for IPv4 and IPv6 addresses, and passes the Pytricia test suite.
  • Includes support for both ipaddress and netaddr and can be easily extended to support other libraries.

Contributing

Feature requests and bug reports are not being accepted without a pull request.

Usage

RouteTable

>>> from ipaddress import IPv4Network
>>> from routelookup.routetable import BaseRouteTable
>>> from routelookup.addrmodule.ipaddress import IpaddressAddrHandler
>>> table = BaseRouteTable(IpaddressAddrHandler())
>>> table["192.168.1.0/24"] = "hello"  # supports strings
>>> table[IPv4Network("192.168.1.128/25")] = "world"  # supports IPv4Network objects too
>>> "10.0.0.1" in table
False
>>> "192.168.1.5" in table
True
>>> table["192.168.1.192/26"]
'world'
>>> table[IPv4Network("192.168.1.192/26")]
'world'
>>> table.lookup_worst("192.168.1.192/26")
'hello'
>>> list(table.items())
[(IPv4Network('192.168.1.0/24'), 'hello'), (IPv4Network('192.168.1.128/25'), 'world')]
>>>

routelookup.pytricia

>>> from routelookup.pytricia import PyTricia
>>> table = PyTricia()
>>> table["10.0.0.0/8"] = "hello"
>>> table["10.1.0.0/16"] = "world"
>>> "10.1.1.0/24" in table
True
>>> table["10.1.1.0/24"]
'world'
>>> table.get_key("10.1.1.0/24")
'10.1.0.0/16'
>>> table.children("10.0.0.0/8")
['10.1.0.0/16']
>>>

Credits

Code in routelookup/python_ipaddress.py is derived from Python's ipaddress standard library module, Copyright (c) 2001 Python Software Foundation; All Rights Reserved.

Project details


Download files

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

Source Distribution

routelookup-1.0.3.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

routelookup-1.0.3-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file routelookup-1.0.3.tar.gz.

File metadata

  • Download URL: routelookup-1.0.3.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.10.16 Linux/6.12.5-200.fc41.x86_64

File hashes

Hashes for routelookup-1.0.3.tar.gz
Algorithm Hash digest
SHA256 fa5871daa7077c599d1b7f123b3db905b28fcb3e073576ffc653988d3e31d26d
MD5 dabef8d9e8b8071e390fe9a875535405
BLAKE2b-256 df3cda9c122640430a1ccace8829004b4047f6c440ad1f75c1ac22287b724756

See more details on using hashes here.

File details

Details for the file routelookup-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: routelookup-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.10.16 Linux/6.12.5-200.fc41.x86_64

File hashes

Hashes for routelookup-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0a727cec393a1d980739bb9bf6593e1caa72b9dc178af26e593313fe26d765c1
MD5 6b9d8a0189c48ff1b15ca9d7d2b65488
BLAKE2b-256 b18919c7dd12452154f14aa8e2d123bb2d8721e439ff6b648c460a855fbda584

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