I have a long array ( /- 1000 entries, 8 million IPs) of IP subnets, and I want to check if (a list of) certain IPs are in that array. The code that I have for it, works. But is quite "slow". Since I have to lookup more than one IP address, I want to make the search faster. Are there any ways to improve the search trough the array?
Example array:
nets = [
'192.168.1.0/24',
'192.168.2.0/24',
'192.168.3.0/24',
]
The code to search:
def search(ip_address):
for net in nets:
if ipaddress.ip_address(ip_address) in ipaddress.ip_network(net):
return True
return False
CodePudding user response:
I would try to express the networks as a list of integer range
s.
from ipaddress import ip_address, ip_network
nets = [
'192.168.1.0/24',
'192.168.2.0/24',
'192.168.3.0/24',
]
ip_ranges = [
(range(int(n.network_address), int(n.broadcast_address)), n)
for n in map(ip_network, nets)
]
ip = int(ip_address('192.168.2.10'))
results = [n for r, n in ip_ranges if ip in r]
print(results)
I did not measure it, but it seemed to be quite snappy with 30,000 networks. (I only did nets * 10000
to beef up the number, and print(len(results))
came up with 10000
pretty much instantly.)