Ehm, how do you check whether an IP address is inside of a specific network with strings? Or how two networks overlap? How many addresses a network has?
Hint: there’s more than /24 networks and IPv6 makes it even more complicated.
Hint: there’s more than /24 networks and IPv6 makes it even more complicated.
>>> str(ipaddress.ip_address('127.0.0.1'))
'127.0.0.1'
>>> str(ipaddress.ip_address('::1'))
'::1'
>>> str(ipaddress.ip_network('192.168.42.0/24'))
'192.168.42.0/24'
But you can do stuff like: >>> ipaddress.ip_address('192.168.42.23') in ipaddress.ip_network('192.168.42.0/24')
True
>>> ipaddress.ip_address('192.168.41.23') in ipaddress.ip_network('192.168.42.0/24')
False
or for addr in ipaddress.ip_network('192.168.42.0/24'):
print(adds)
Etc. I strongly suggest to have a look at http://docs.python.org/dev/howto/ipaddress.html#ipaddress-ho... to have an overview over it’s functionality – unfortunately the API docs aren’t done yet, but will be for 3.3 final.edit fixed code markup