class ipaddress.IPv4Network(address, strict=True)
Construct an IPv4 network definition. address can be one of the following:
-
A string consisting of an IP address and an optional mask, separated by a slash (
/
). The IP address is the network address, and the mask can be either a single number, which means it’s a prefix, or a string representation of an IPv4 address. If it’s the latter, the mask is interpreted as a net mask if it starts with a non-zero field, or as a host mask if it starts with a zero field. If no mask is provided, it’s considered to be/32
.For example, the following address specifications are equivalent:
192.168.1.0/24
,192.168.1.0/255.255.255.0
and192.168.1.0/0.0.0.255
. - An integer that fits into 32 bits. This is equivalent to a single-address network, with the network address being address and the mask being
/32
. - An integer packed into a
bytes
object of length 4, big-endian. The interpretation is similar to an integer address. - A two-tuple of an address description and a netmask, where the address description is either a string, a 32-bits integer, a 4-bytes packed integer, or an existing IPv4Address object; and the netmask is either an integer representing the prefix length (e.g.
24
) or a string representing the prefix mask (e.g.255.255.255.0
).
An AddressValueError
is raised if address is not a valid IPv4 address. A NetmaskValueError
is raised if the mask is not valid for an IPv4 address.
If strict is True
and host bits are set in the supplied address, then ValueError
is raised. Otherwise, the host bits are masked out to determine the appropriate network address.
Unless stated otherwise, all network methods accepting other network/address objects will raise TypeError
if the argument’s IP version is incompatible to self
Changed in version 3.5: Added the two-tuple form for the address constructor parameter.
-
version
-
max_prefixlen
-
Refer to the corresponding attribute documentation in
IPv4Address
-
is_multicast
-
is_private
-
is_unspecified
-
is_reserved
-
is_loopback
-
is_link_local
-
These attributes are true for the network as a whole if they are true for both the network address and the broadcast address
-
network_address
-
The network address for the network. The network address and the prefix length together uniquely define a network.
-
broadcast_address
-
The broadcast address for the network. Packets sent to the broadcast address should be received by every host on the network.
-
hostmask
-
The host mask, as a string.
-
with_prefixlen
-
compressed
-
exploded
-
A string representation of the network, with the mask in prefix notation.
with_prefixlen
andcompressed
are always the same asstr(network)
.exploded
uses the exploded form the network address.
-
with_netmask
-
A string representation of the network, with the mask in net mask notation.
-
with_hostmask
-
A string representation of the network, with the mask in host mask notation.
-
num_addresses
-
The total number of addresses in the network.
-
prefixlen
-
Length of the network prefix, in bits.
-
hosts()
-
Returns an iterator over the usable hosts in the network. The usable hosts are all the IP addresses that belong to the network, except the network address itself and the network broadcast address.
>>> list(ip_network('192.0.2.0/29').hosts()) [IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'), IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'), IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
-
overlaps(other)
-
True
if this network is partly or wholly contained in other or other is wholly contained in this network.
-
address_exclude(network)
-
Computes the network definitions resulting from removing the given network from this one. Returns an iterator of network objects. Raises
ValueError
if network is not completely contained in this network.>>> n1 = ip_network('192.0.2.0/28') >>> n2 = ip_network('192.0.2.1/32') >>> list(n1.address_exclude(n2)) [IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'), IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')]
-
subnets(prefixlen_diff=1, new_prefix=None)
-
The subnets that join to make the current network definition, depending on the argument values. prefixlen_diff is the amount our prefix length should be increased by. new_prefix is the desired new prefix of the subnets; it must be larger than our prefix. One and only one of prefixlen_diff and new_prefix must be set. Returns an iterator of network objects.
>>> list(ip_network('192.0.2.0/24').subnets()) [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')] >>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2)) [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'), IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')] >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26)) [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'), IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')] >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23)) Traceback (most recent call last): File "<stdin>", line 1, in <module> raise ValueError('new prefix must be longer') ValueError: new prefix must be longer >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25)) [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
-
supernet(prefixlen_diff=1, new_prefix=None)
-
The supernet containing this network definition, depending on the argument values. prefixlen_diff is the amount our prefix length should be decreased by. new_prefix is the desired new prefix of the supernet; it must be smaller than our prefix. One and only one of prefixlen_diff and new_prefix must be set. Returns a single network object.
>>> ip_network('192.0.2.0/24').supernet() IPv4Network('192.0.2.0/23') >>> ip_network('192.0.2.0/24').supernet(prefixlen_diff=2) IPv4Network('192.0.0.0/22') >>> ip_network('192.0.2.0/24').supernet(new_prefix=20) IPv4Network('192.0.0.0/20')
-
compare_networks(other)
-
Compare this network to other. In this comparison only the network addresses are considered; host bits aren’t. Returns either
-1
,0
or1
.>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32')) -1 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.0/32')) 1 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.1/32')) 0
Please login to continue.