The ipaddress module includes classes for working with IPv4 and IPv6 network addresses. The classes support validation, finding addresses and hosts on a network, and other common operations.
The most basic object represents the network address itself. Pass a string, integer, or byte sequence to ip_address() to construct an address. The return value will be a IPv4Address or IPv6Address instance, depending on the type of address being used.
In [3]:
import binascii
import ipaddress
ADDRESSES = [
'10.9.0.6',
'fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa',
]
for ip in ADDRESSES:
addr = ipaddress.ip_address(ip)
print('{!r}'.format(addr))
print(' IP version:', addr.version)
print(' is private:', addr.is_private)
print(' packed form:', binascii.hexlify(addr.packed))
print(' integer:', int(addr))
print()
Both classes can provide various representations of the address for different purposes, as well as answer basic assertions such as whether the address is reserved for multicast communication or if it is on a private network.
A network is defined by a range of addresses. It is usually expressed with a base address and a mask indicating which portions of the address represent the network, and which portions are remaining to represent addresses on that network. The mask can be expressed explicitly, or using a prefix length value as in the example below.
In [4]:
import ipaddress
NETWORKS = [
'10.9.0.0/24',
'fdfd:87b5:b475:5e3e::/64',
]
for n in NETWORKS:
net = ipaddress.ip_network(n)
print('{!r}'.format(net))
print(' is private:', net.is_private)
print(' broadcast:', net.broadcast_address)
print(' compressed:', net.compressed)
print(' with netmask:', net.with_netmask)
print(' with hostmask:', net.with_hostmask)
print(' num addresses:', net.num_addresses)
print()
As with addresses, there are two network classes for IPv4 and IPv6 networks. Each class provides properties or methods for accessing values associated with the network such as the broadcast address and the addresses on the network available for hosts to use.
A network instance is iterable, and yields the addresses on the network.
In [5]:
import ipaddress
NETWORKS = [
'10.9.0.0/24',
'fdfd:87b5:b475:5e3e::/64',
]
for n in NETWORKS:
net = ipaddress.ip_network(n)
print('{!r}'.format(net))
for i, ip in zip(range(3), net):
print(ip)
print()
Iterating over the network yields addresses, but not all of them are valid for hosts. For example, the base address of the network and the broadcast address are both included. To find the addresses that can be used by regular hosts on the network, use the hosts() method, which produces a generator.
In [6]:
import ipaddress
NETWORKS = [
'10.9.0.0/24',
'fdfd:87b5:b475:5e3e::/64',
]
for n in NETWORKS:
net = ipaddress.ip_network(n)
print('{!r}'.format(net))
for i, ip in zip(range(3), net.hosts()):
print(ip)
print()
In addition to the iterator protocol, networks support the in operator to determine if an address is part of a network.
In [7]:
import ipaddress
NETWORKS = [
ipaddress.ip_network('10.9.0.0/24'),
ipaddress.ip_network('fdfd:87b5:b475:5e3e::/64'),
]
ADDRESSES = [
ipaddress.ip_address('10.9.0.6'),
ipaddress.ip_address('10.7.0.31'),
ipaddress.ip_address(
'fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa'
),
ipaddress.ip_address('fe80::3840:c439:b25e:63b0'),
]
for ip in ADDRESSES:
for net in NETWORKS:
if ip in net:
print('{}\nis on {}'.format(ip, net))
break
else:
print('{}\nis not on a known network'.format(ip))
print()
A network interface represents a specific address on a network and can be represented by a host address and a network prefix or netmask.
In [8]:
import ipaddress
ADDRESSES = [
'10.9.0.6/24',
'fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa/64',
]
for ip in ADDRESSES:
iface = ipaddress.ip_interface(ip)
print('{!r}'.format(iface))
print('network:\n ', iface.network)
print('ip:\n ', iface.ip)
print('IP with prefixlen:\n ', iface.with_prefixlen)
print('netmask:\n ', iface.with_netmask)
print('hostmask:\n ', iface.with_hostmask)
print()
The interface object has properties to access the full network and address separately, as well as several different ways to express the interface and network mask.
In [ ]: