3 ms·
You can’t hide the complexity of doing that right but OTOH the ipaddress objects have useful str() methods: >>> str(ipaddress.ip_address('127.0.0.1')) '127
by hynek 14y ago
You can’t hide the complexity of doing that right but OTOH the ipaddress objects have useful str() methods:
>>> 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-howto 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
- sho_hn 14y agoJust for the record, I agree that the problem space is such that a utility class is warranted having written code dealing with IP addresses in the past; I was just speculating about the OP's thoughts :).