Skip to main content

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

Read the documentation here.

Installation

  • Using pip:

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

$ git clone https://github.com/Figglewatts/cidr-trie.git
$ cd cidr-trie
$ python setup.py install
  • From source (PyPI):

$ wget https://files.pythonhosted.org/packages/6b/53/118c09dc2c294f41b12007634d53ed33219d15366ea8a1903fb98eb47c25/cidr_trie-1.0.tar.gz
$ tar xvf cidr_trie-1.0.tar.gz
$ cd cidr_trie-1.0
$ python setup.py install

Usage

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("0.0.0.0/0", "Internet")
trie.insert("32.0.0.0/9", "RIR-A")
trie.insert("32.128.0.0/9", "RIR-B")
trie.insert("32.32.0.0/16", "another")
trie.insert("32.32.32.0/24", "third")
trie.insert("32.32.32.32/32", "you")
trie.insert("192.168.0.1/32", "totally different")
trie.insert("33.0.0.0/8", "RIR3")
trie.insert("64.0.0.0/8", "RIR2")

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

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

# nodes: ['Internet', 'totally different']
trie.find_all("192.168.0.1/32")

# nodes: ['Internet', 'RIR-B']
trie.find_all("32.192.0.0/10")

# --- 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']
trie.find_all("1234:1001:1920:2000:2020::/128")

# nodes: ['Internet', 'Test', 'Another one', 'A third', 'A different one']
trie.find_all("1234:1001:1920::ffff")

Metadata

Release files for cidr-trie 3.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cidr-trie 3.1.2
File Size Uploaded
cidr_trie-3.1.2.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cidr-trie 3.1.2
File Interpreter ABI Platform
cidr_trie-3.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 17.0 kB

Release files / cidr_trie-3.1.2.tar.gz

Download URL cidr_trie-3.1.2.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1b1193d3d9457dea63ec881471b3c3145bc47d464191eafb3418f59d9d5e1fcb
BLAKE2b-256 checksum
How to use checksums
0ab733eb7d9288708b0ad941cb5eff0fc86338dc54171c1151e3ceb528b5f85c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release files / cidr_trie-3.1.2-py3-none-any.whl

Download URL cidr_trie-3.1.2-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2bc92c693cc4d6b1e0d1682921abd84d9bed2f99e2bc6cd6bc28bc1cfa24f1f2
BLAKE2b-256 checksum
How to use checksums
b6e5ba56d10c3e8b1ac2fe4c746e01f886bafdc3182f8b0c0804090e8c017536
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release history Release notifications | RSS feed

This release

3.1.2 This release

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2

2 release files

2.1.4

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1

2 release files

2.0

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page