# 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](https://github.com/jsommers/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
```pycon
>>> 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
```pycon
>>> 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.
Raw data
{
"_id": null,
"home_page": null,
"name": "routelookup",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "cidr, ip, lookup, prefix, route, routing table",
"author": "carter1643",
"author_email": "carter1643@pm.me",
"download_url": "https://files.pythonhosted.org/packages/df/3c/da9c122640430a1ccace8829004b4047f6c440ad1f75c1ac22287b724756/routelookup-1.0.3.tar.gz",
"platform": null,
"description": "# routelookup\nA simple, pure Python implementation of routing table-style IP prefix lookups. Compatible with Python 3.10 and higher.\n\n## Features\n- Portable\n- No dependencies outside the standard library\n- Longest-prefix lookup is O(1) on average\n- `routelookup.pytricia` is a drop-in replacement for [Pytricia](https://github.com/jsommers/pytricia) for IPv4 and IPv6 addresses, and passes the \nPytricia test suite.\n- Includes support for both `ipaddress` and `netaddr` and can be easily extended to support other libraries.\n\n## Contributing\nFeature requests and bug reports are not being accepted without a pull request.\n\n## Usage\n### RouteTable\n\n```pycon\n>>> from ipaddress import IPv4Network\n>>> from routelookup.routetable import BaseRouteTable\n>>> from routelookup.addrmodule.ipaddress import IpaddressAddrHandler\n>>> table = BaseRouteTable(IpaddressAddrHandler())\n>>> table[\"192.168.1.0/24\"] = \"hello\" # supports strings\n>>> table[IPv4Network(\"192.168.1.128/25\")] = \"world\" # supports IPv4Network objects too\n>>> \"10.0.0.1\" in table\nFalse\n>>> \"192.168.1.5\" in table\nTrue\n>>> table[\"192.168.1.192/26\"]\n'world'\n>>> table[IPv4Network(\"192.168.1.192/26\")]\n'world'\n>>> table.lookup_worst(\"192.168.1.192/26\")\n'hello'\n>>> list(table.items())\n[(IPv4Network('192.168.1.0/24'), 'hello'), (IPv4Network('192.168.1.128/25'), 'world')]\n>>>\n```\n\n### routelookup.pytricia\n```pycon\n>>> from routelookup.pytricia import PyTricia\n>>> table = PyTricia()\n>>> table[\"10.0.0.0/8\"] = \"hello\"\n>>> table[\"10.1.0.0/16\"] = \"world\"\n>>> \"10.1.1.0/24\" in table\nTrue\n>>> table[\"10.1.1.0/24\"]\n'world'\n>>> table.get_key(\"10.1.1.0/24\")\n'10.1.0.0/16'\n>>> table.children(\"10.0.0.0/8\")\n['10.1.0.0/16']\n>>>\n```\n\n## Credits\nCode in routelookup/python_ipaddress.py is derived from Python's ipaddress standard library module, Copyright (c) 2001 Python Software Foundation; All Rights Reserved.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Routing table-style IP prefix lookups",
"version": "1.0.3",
"project_urls": {
"repository": "https://github.com/carter1643/routelookup"
},
"split_keywords": [
"cidr",
" ip",
" lookup",
" prefix",
" route",
" routing table"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b18919c7dd12452154f14aa8e2d123bb2d8721e439ff6b648c460a855fbda584",
"md5": "6b9d8a0189c48ff1b15ca9d7d2b65488",
"sha256": "0a727cec393a1d980739bb9bf6593e1caa72b9dc178af26e593313fe26d765c1"
},
"downloads": -1,
"filename": "routelookup-1.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6b9d8a0189c48ff1b15ca9d7d2b65488",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 11229,
"upload_time": "2024-12-21T22:07:49",
"upload_time_iso_8601": "2024-12-21T22:07:49.646863Z",
"url": "https://files.pythonhosted.org/packages/b1/89/19c7dd12452154f14aa8e2d123bb2d8721e439ff6b648c460a855fbda584/routelookup-1.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "df3cda9c122640430a1ccace8829004b4047f6c440ad1f75c1ac22287b724756",
"md5": "dabef8d9e8b8071e390fe9a875535405",
"sha256": "fa5871daa7077c599d1b7f123b3db905b28fcb3e073576ffc653988d3e31d26d"
},
"downloads": -1,
"filename": "routelookup-1.0.3.tar.gz",
"has_sig": false,
"md5_digest": "dabef8d9e8b8071e390fe9a875535405",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 8667,
"upload_time": "2024-12-21T22:07:50",
"upload_time_iso_8601": "2024-12-21T22:07:50.532287Z",
"url": "https://files.pythonhosted.org/packages/df/3c/da9c122640430a1ccace8829004b4047f6c440ad1f75c1ac22287b724756/routelookup-1.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-21 22:07:50",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "carter1643",
"github_project": "routelookup",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "routelookup"
}