Official mirror of git.bro.org/pysubnettree.git
C++ C Python CMake Makefile
Clone or download
Latest commit f89fe69 Aug 8, 2018
Permalink
Failed to load latest commit information.
cmake @ 4cc3e34 Updating submodule(s). Aug 8, 2018
testing Cleanup tests for the prefixes() function Oct 4, 2016
.gitignore Updated setup.py to work with setuptools (pypi). Apr 1, 2014
.gitmodules Change submodules to fixed URL. Sep 23, 2013
.travis.yml Configure Travis CI email recipients and build branches Feb 22, 2018
.update-changes.cfg Fixing version number in setup.cfg. Oct 14, 2013
CHANGES Fix deprecation warning for swig_add_module Aug 6, 2018
CMakeLists.txt Fix deprecation warning for swig_add_module Aug 6, 2018
COPYING Updating copyright notice. Oct 8, 2013
MANIFEST.in Update MANIFEST.in May 30, 2014
Makefile Add dist target to Makefile. May 8, 2015
README Fix deprecation warning for swig_add_module Aug 6, 2018
README.rst Merge branch 'docs_changes' of https://github.com/dsalisbury/pysubnet… Mar 23, 2015
SubnetTree.cc Fix the prefixes() function to compile and work on Python 3 Oct 4, 2016
SubnetTree.h Added prefixes() method to return all prefixes in the tree as a set of Jan 23, 2016
SubnetTree.i Initial import Nov 4, 2010
SubnetTree.py Merge branch 'topic/jroyalty/prefixset' of https://github.com/jroyalt… Jan 25, 2016
SubnetTree_wrap.cc Merge branch 'topic/jroyalty/prefixset' of https://github.com/jroyalt… Jan 25, 2016
VERSION Fix deprecation warning for swig_add_module Aug 6, 2018
btest Merge remote-tracking branch 'origin/fastpath' Jul 5, 2012
configure Fix configure script to exit with non-zero status on error Aug 1, 2012
patricia.c Fix some issues reported by coverity Mar 23, 2018
patricia.h Apply patch from ticket #750 Mar 29, 2012
setup.py Fix deprecation warning for swig_add_module Aug 6, 2018

README.rst

PySubnetTree - A Python Module for CIDR Lookups

.. rst-class:: opening

    The PySubnetTree package provides a Python data structure
    ``SubnetTree`` which maps subnets given in `CIDR
    <http://tools.ietf.org/html/rfc4632>`_ notation (incl.
    corresponding IPv6 versions) to Python objects. Lookups are
    performed by longest-prefix matching.


Download

You can find the latest PySubnetTree release for download at http://www.bro.org/download.

PySubnetTree's git repository is located at git://git.bro.org/pysubnettree.git. You can browse the repository here.

This document describes PySubnetTree 0.27-18. 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
>>> 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

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 2.6 or newer.

Installation

Installation is pretty simple:

> python setup.py install