My question: Is a multi-network container type like netaddr.IPSet in scope for ipaddress?
I happen to be exploring a case of checking whether requests are coming from any major cloud provider’s datacenter, which means checking against a few thousand ip ranges. The simple version of this is pretty expensive (a few milliseconds for 3k non-overlapping networks) with
ipaddress when you have thousands of networks to check:
any(ip in network for network in networks)
An O(1) implementation with comparable performance (about 2x slower, but identical O(1) scaling) can be achieved with the standard library, assuming you are starting with a set or dict of networks:
def ip_in_network_set(ip, network_set): """Return whether `ip` is in a set of networks This is O(1) regardless of the size of network_set """ check_net = ipaddress.ip_network(ip) while check_net.prefixlen: if check_net in network_set: return True check_net = check_net.supernet(1) return False
Does it make sense for the standard library to include a similar construct of higher level “collection of ips” like IPvXNetwork, but not necessarily represented by a single CIDR string? It would logically have the various iterator,
__contains__, overlap methods, but not those that refer to a single CIDR string (prefixlen, etc.).