python3 -m pip install cidr-trie
>>> from cidr_trie import PatriciaTrie
>>> ip_owner = PatriciaTrie()
>>> ip_owner.insert("10.0.0.0/8", "LAN-A")
>>> ip_owner.insert("172.16.0.0/12", "LAN-B")
>>> ip_owner.insert("192.168.0.0/16", "LAN-C")
>>> ip_owner.insert("192.168.1.0/24", "LAN-C-Office-Toronto")
>>> ip_owner.insert("104.16.0.0/12", "Internet-Cloudflare")
>>> ip_owner.insert("31.171.152.0/22", "Internet-HostileVPN")
>>> ip_owner.find_all("31.171.152.25")
[('31.171.152.0/22', 'Hostile VPN')]
>>> ip_owner.find_all("192.168.1.13")
[('192.168.0.0/16', 'LAN-C'), ('192.168.1.0/24', 'LAN-C-Office-Toronto')]
>>> ', '.join((v[1] for v in ip_owner.find_all("192.168.1.13")))
'LAN C, LAN-C-Office-Toronto'
Więcej informacji:
Skompresowane drzewo trie, Store CIDR IP addresses (both v4 and v6) in a trie for easy lookup