/
Depends on
- dh-python (dh-sequence-python3)
- python3-defaults (python3-all-dev)
- setuptools (python3-setuptools)
- swig (swig)
- debhelper-compat
README (full)
.. -- mode: rst-mode --
PySubnetTree - A Python Module for CIDR Lookups
Download
You can find the latest PySubnetTree release for download at https://www.zeek.org/download.
PySubnetTree's git repository is located at https://github.com/zeek/pysubnettree
This document describes PySubnetTree 0.38.1. See the CHANGES file for version history.
Example
A simple example which associates CIDR prefixes with strings::
>>> import SubnetTree >>> t = SubnetTree.SubnetTree() >>> t["10.1.0.0/16"] = "Network 1" >>> t["10.1.42.0/24"] = "Network 1, Subnet 42" >>> print("10.1.42.1" in t) True >>> print(t["10.1.42.1"]) Network 1, Subnet 42 >>> print(t["10.1.43.1"]) Network 1 >>> print("10.20.1.1" in t) False >>> t.search_all("10.1.42.1") ['Network 1, Subnet 42', 'Network 1'] >>> try: ... print(t["10.20.1.1"]) ... except KeyError as err: ... print("Error: %s not found" % err) Error: '10.20.1.1' not found
PySubnetTree also supports IPv6 addresses and prefixes::
>>> import SubnetTree >>> t = SubnetTree.SubnetTree() >>> t["2001:db8::/32"] = "Company 1" >>> t["2001:db8:4000::/48"] = "Company 1, Site 1" >>> t["2001:db8:4000:abcd::"] Company 1, Site 1 >>> t["2001:db8:fe:1234::"] Company 1 >>> t.search_all("2001:db8:4000:abcd::1") ['Company 1, Site 1', 'Company 1']
By default, CIDR prefixes and IP addresses are given as strings. Alternatively, a SubnetTree object can be switched into *binary mode*, in which single addresses are passed in the form of packed binary strings as, e.g., returned by socket.inet_aton::
>>> t.get_binary_lookup_mode() False >>> t.set_binary_lookup_mode(True) >>> t.get_binary_lookup_mode() True >>> import socket >>> print(t[socket.inet_aton("10.1.42.1")]) Network 1, Subnet 42
A SubnetTree also provides methods insert(prefix,object=None) for insertion of prefixes (object can be skipped to use the tree like a set), and remove(prefix) for removing entries (remove performs an _exact match rather than longest-prefix).
Internally, the CIDR prefixes of a SubnetTree are managed by a Patricia tree data structure and lookups are therefore efficient even with a large number of prefixes.
PySubnetTree comes with a BSD license.
Prerequisites
This package requires Python 3.9 or newer.
Installation
Installation is pretty simple::
> python3 setup.py install
| dir | .github/ |
| dir | cmake/ |
| dir | debian/ |
| dir | include/ |
| dir | testing/ |
| file | .clang-format |
| file | .clang-format-ignore |
| file | .cmake-format.json |
| file | .gitignore |
| file | .gitmodules |
| file | .pre-commit-config.yaml |
| file | .update-changes.cfg |
| file | CHANGES |
| file | CMakeLists.txt |
| file | COPYING |
| file | MANIFEST.in |
| file | Makefile |
| file | README |
| file | README.rst |
| file | SubnetTree.cc |
| file | SubnetTree.i |
| file | VERSION |
| file | configure |
| file | patricia.c |
| file | pyproject.toml |
| file | setup.py |