Python 3.3 ipaddress module Awesomeness!

Python 3.3 was released yesterday, Sept. 29, 2012. It has a wonderfully geeky module for IP address.

Take a look at some of its usages..
Casting / Conversions

>>> str(ipaddress.IPv4Address('192.168.0.1'))
'192.168.0.1'
>>> int(ipaddress.IPv4Address('192.168.0.1'))
3232235521
>>> str(ipaddress.IPv6Address('::1'))
'::1'
>>> int(ipaddress.IPv6Address('::1'))
1

Arithmetic Operations

>>> IPv4Address('127.0.0.2') + 3
IPv4Address('127.0.0.5')
>>> IPv4Address('127.0.0.2') - 3
IPv4Address('126.255.255.255')
>>> IPv4Address('255.255.255.255') + 1
Traceback (most recent call last):
  File "", line 1, in 
ipaddress.AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address

Wish I could code in Python 🙁

References

Leave a Reply

Your email address will not be published. Required fields are marked *