Skip to content
A Python Module for CIDR Lookups
C++ C Python CMake Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
cmake @ 1dd6a7b Updating submodule(s). Sep 4, 2019
testing Update for a directory name change May 24, 2019
.gitignore Updated to work with setuptools (pypi). Apr 1, 2014
.gitmodules Update submodules to use Dec 6, 2018
.travis.yml Drop use of Python 2.6 for Travis CI tests Aug 1, 2019
.update-changes.cfg Fixing version number in setup.cfg. Oct 14, 2013
CHANGES Updating CHANGES and VERSION. Aug 8, 2019
CMakeLists.txt Fix deprecation warning for swig_add_module Aug 6, 2018
COPYING Updating copyright notice. Oct 8, 2013 Update May 30, 2014
Makefile Check for twine existence in `make upload` Apr 12, 2019
README Updating CHANGES and VERSION. Aug 8, 2019
README.rst Merge branch 'docs_changes' of… Mar 23, 2015 Merge branch 'seg_bad_mask' of Mar 22, 2019
SubnetTree.h GH-14: Fix segfault in lookup operations with Python3 Apr 12, 2019
SubnetTree.i Initial import Nov 4, 2010 Merge branch 'topic/jroyalty/prefixset' of… Jan 25, 2016 Merge branch 'topic/jroyalty/prefixset' of… Jan 25, 2016
VERSION Updating CHANGES and VERSION. Aug 8, 2019
btest Merge remote-tracking branch 'origin/fastpath' Jul 5, 2012
configure Fix python3 compatibility of ./configure script Mar 22, 2019
patricia.c Fix some issues reported by coverity Mar 23, 2018
patricia.h Apply patch from ticket #750 Mar 29, 2012 Updating CHANGES and VERSION. Aug 8, 2019


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
    <>`_ notation (incl.
    corresponding IPv6 versions) to Python objects. Lookups are
    performed by longest-prefix matching.


You can find the latest PySubnetTree release for download at

PySubnetTree's git repository is located at

This document describes PySubnetTree 0.31. See the CHANGES file for version history.


A simple example which associates CIDR prefixes with strings:

>>> import SubnetTree
>>> t = SubnetTree.SubnetTree()
>>> t[""] = "Network 1"
>>> t[""] = "Network 1, Subnet 42"
>>> print("" in t)
>>> print(t[""])
Network 1, Subnet 42
>>> print(t[""])
Network 1
>>> print("" in t)
>>> try:
...     print(t[""])
... except KeyError as err:
...     print("Error: %s not found" % err)
Error: '' 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()
>>> t.set_binary_lookup_mode(True)
>>> t.get_binary_lookup_mode()
>>> import socket
>>> print(t[socket.inet_aton("")])
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.


This package requires Python 2.6 or newer.


Installation is pretty simple:

> python install
You can’t perform that action at this time.