Skip to main content

Store/search CIDR prefixes in a trie structure.

Project description

Store CIDR IP addresses (both v4 and v6) in a trie for easy lookup.

Read the documentation here.


  • Using pip:

$ pip install cidr-trie
  • From source (Git):

$ git clone
$ cd cidr-trie
$ python install
  • From source (PyPI):

$ wget
$ tar xvf cidr_trie-1.0.tar.gz
$ cd cidr_trie-1.0
$ python install


cidr-trie can be used to build a trie of IP networks, storing data on each node. The stored data can be of any type. Shown here is an example of building both IPv4 and IPv6 tries with data, and then retrieving data from both tries.

from cidr_trie import PatriciaTrie

# --- supports IPv4 ---
trie = PatriciaTrie()
trie.insert("", "Internet")
trie.insert("", "RIR-A")
trie.insert("", "RIR-B")
trie.insert("", "another")
trie.insert("", "third")
trie.insert("", "you")
trie.insert("", "totally different")
trie.insert("", "RIR3")
trie.insert("", "RIR2")

# nodes: ['Internet', 'RIR-A', 'another', 'third', 'you']
nodes_for_prefix = trie.find_all("")

# prints "Internet, RIR-A, another, third, you"
print(', '.join(n.value for n in nodes_for_prefix))

# nodes: ['Internet', 'totally different']

# nodes: ['Internet', 'RIR-B']

# --- supports IPv6 ---
trie = PatriciaTrie()
trie.insert("::/0", "Internet")
trie.insert("1234::/16", "Test")
trie.insert("1234:1001::/32", "Another one")
trie.insert("1234:1001:1920::/48", "A third")
trie.insert("1234:1001:1920:2000:2020::/96", "A fourth")
trie.insert("1234:1001:1920::ffff", "A different one")

# nodes: ['Internet', 'Test', 'Another one', 'A third', 'A fourth']

# nodes: ['Internet', 'Test', 'Another one', 'A third', 'A different one']

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

cidr_trie-3.1.2.tar.gz (8.0 kB view hashes)

Uploaded source

Built Distribution

cidr_trie-3.1.2-py3-none-any.whl (9.0 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page