Python 3.3+ipaddressterm
ipaddress.IPv4Address
addresses
The IPv4 address class: it validates the text on creation and supports comparison, integer arithmetic and properties such as is_private.
Syntax
ipaddress.IPv4Address(address)
The constructor is stricter than ip_address(): it raises AddressValueError (a ValueError subclass) and never accepts IPv6. Addresses sort and add to integers: IPv4Address('10.0.0.255') + 1 gives 10.0.1.0, and int() returns the address number. Comparing it with an IPv6Address via < raises TypeError, and == against the IPv6-mapped '::ffff:1.2.3.4' is False.
Examples
import ipaddress
a = ipaddress.IPv4Address('10.0.0.255')
print(a + 1, int(a)) # → 10.0.1.0 167772415
print(a < ipaddress.IPv4Address('10.0.1.0')) # → True
try:
ipaddress.IPv4Address('10.0.0.256')
except ipaddress.AddressValueError as e:
print(e) # → Octet 256 (> 255) not permitted in '10.0.0.256'
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy