以三种结构存储/搜索cidr前缀。
cidr-trie的Python项目详细描述
在trie中存储cidr ip地址(v4和v6),以便于查找。
阅读文档here。
安装
- 使用PIP:
$ pip install cidr-trie
- 来自源(git):
$ git clone https://github.com/Figglewatts/cidr-trie.git
$ cd cidr-trie
$ python setup.py install
- 来自源(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
用法
cidr-trie可用于构建ip网络的trie,存储数据 在每个节点上。存储的数据可以是任何类型。 这里显示了一个使用数据构建ipv4和ipv6尝试的示例, 然后从两次尝试中检索数据。
fromcidr_trieimportPatriciaTrie# --- 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.valueforninnodes_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")