Skip to content

ipaddress

Functions to create and manipulate IPv4 and IPv6 addresses and networks.

import ipaddress

Functions

ipaddress.ip_address(address: str) -> object

Parses a string as an IPv4 or IPv6 address.

ipaddress.ip_network(address: str, strict: bool = True) -> object

Parses a string as an IPv4 or IPv6 network.

ipaddress.ip_interface(address: str) -> object

Parses a string as an IPv4 or IPv6 interface.

ipaddress.collapse_addresses(addresses: list[object]) -> list[object]

Collapses addresses and networks into the smallest set of CIDR blocks.

ipaddress.summarize_address_range(first: object, last: object) -> list[object]

Summarizes an address range as the smallest set of CIDR blocks.

IPv4Address

Represents an IPv4 address.

Properties

Name Type Description
version int Gets the IP version number.
max_prefixlen int Gets the maximum prefix length for IPv4 addresses.
is_private bool Gets whether the address is in a private-use range.
is_loopback bool Gets whether the address is a loopback address.
is_multicast bool Gets whether the address is a multicast address.
is_reserved bool Gets whether the address is in a reserved range.
is_link_local bool Gets whether the address is a link-local address.
is_global bool Gets whether the address is globally reachable.
is_unspecified bool Gets whether the address is the unspecified address.
packed bytes Gets the packed binary representation of the address.
compressed str Gets the canonical string form of the address.

to_int() -> int64

Returns the integer value of the address.

__str__() -> str

repr() uses the same method. Returns the dotted-decimal string form of the address.

IPv4Network

Represents an IPv4 network.

Properties

Name Type Description
version int Gets the IP version number.
prefixlen int Gets the network prefix length.
max_prefixlen int Gets the maximum prefix length for IPv4 networks.
network_address IPv4Address Gets the network address.
broadcast_address IPv4Address Gets the broadcast address.
netmask IPv4Address Gets the network mask.
hostmask IPv4Address Gets the host mask.
num_addresses int64 Gets the number of addresses in the network.
is_private bool Gets whether the network is in a private-use range.
is_global bool Gets whether the network is globally reachable.
is_reserved bool Gets whether the network is in a reserved range.
is_loopback bool Gets whether the network is a loopback network.
is_multicast bool Gets whether the network is a multicast network.
is_link_local bool Gets whether the network is link-local.
with_prefixlen str Gets the network in address/prefix notation.
with_netmask str Gets the network in address/netmask notation.
with_hostmask str Gets the network in address/hostmask notation.

hosts() -> IEnumerable[IPv4Address]

Iterates over usable host addresses in the network.

contains(address: IPv4Address) -> bool

Determines whether the network contains the specified address.

overlaps(other: IPv4Network) -> bool

Determines whether this network overlaps another network.

subnets(prefixlen_diff: int = 1, new_prefix: int | None = None) -> list[IPv4Network]

Splits the network into subnets.

supernet(prefixlen_diff: int = 1, new_prefix: int | None = None) -> IPv4Network

Returns the containing supernet.

subnet_of(other: IPv4Network) -> bool

Determines whether this network is a subnet of another network.

supernet_of(other: IPv4Network) -> bool

Determines whether this network is a supernet of another network.

__str__() -> str

repr() uses the same method. Returns the CIDR string form of the network.

IPv6Address

Represents an IPv6 address.

Properties

Name Type Description
version int Gets the IP version number.
max_prefixlen int Gets the maximum prefix length for IPv6 addresses.
is_private bool Gets whether the address is in a private-use range.
is_loopback bool Gets whether the address is a loopback address.
is_multicast bool Gets whether the address is a multicast address.
is_link_local bool Gets whether the address is link-local.
is_site_local bool Gets whether the address is site-local.
is_unspecified bool Gets whether the address is the unspecified address.
is_reserved bool Gets whether the address is in a reserved range.
is_global bool Gets whether the address is globally reachable.
packed bytes Gets the packed binary representation of the address.
compressed str Gets the compressed string form of the address.
exploded str Gets the fully expanded hexadecimal string form of the address.
ipv4_mapped IPv4Address \| None Gets the embedded IPv4 address if this is an IPv4-mapped IPv6 address.

to_int() -> BigInteger

Returns the integer value of the address.

__str__() -> str

repr() uses the same method. Returns the compressed string form of the address.

IPv6Network

Represents an IPv6 network.

Properties

Name Type Description
version int Gets the IP version number.
prefixlen int Gets the network prefix length.
max_prefixlen int Gets the maximum prefix length for IPv6 networks.
network_address IPv6Address Gets the network address.
broadcast_address IPv6Address Gets the last address in the network.
netmask IPv6Address Gets the network mask.
hostmask IPv6Address Gets the host mask.
num_addresses BigInteger Gets the number of addresses in the network.
is_private bool Gets whether the network is in a private-use range.
is_global bool Gets whether the network is globally reachable.
is_loopback bool Gets whether the network is a loopback network.
is_multicast bool Gets whether the network is a multicast network.
is_reserved bool Gets whether the network is in a reserved range.
is_link_local bool Gets whether the network is link-local.
with_prefixlen str Gets the network in address/prefix notation.
with_netmask str Gets the network in address/netmask notation.
with_hostmask str Gets the network in address/hostmask notation.

contains(address: IPv6Address) -> bool

Determines whether the network contains the specified address.

overlaps(other: IPv6Network) -> bool

Determines whether this network overlaps another network.

hosts() -> IEnumerable[IPv6Address]

Iterates over usable host addresses in the network.

subnets(prefixlen_diff: int = 1, new_prefix: int | None = None) -> list[IPv6Network]

Splits the network into subnets.

supernet(prefixlen_diff: int = 1, new_prefix: int | None = None) -> IPv6Network

Returns the containing supernet.

subnet_of(other: IPv6Network) -> bool

Determines whether this network is a subnet of another network.

supernet_of(other: IPv6Network) -> bool

Determines whether this network is a supernet of another network.

__str__() -> str

repr() uses the same method. Returns the CIDR string form of the network.

IPv4Interface

Represents an IPv4 interface with an address and network.

Properties

Name Type Description
ip IPv4Address Gets the interface address.
network IPv4Network Gets the associated network.
version int Gets the IP version number.
prefixlen int Gets the interface prefix length.
with_prefixlen str Gets the interface in address/prefix notation.
with_netmask str Gets the interface in address/netmask notation.
with_hostmask str Gets the interface in address/hostmask notation.

__str__() -> str

repr() uses the same method. Returns the address/prefix string form of the interface.

IPv6Interface

Represents an IPv6 interface with an address and network.

Properties

Name Type Description
ip IPv6Address Gets the interface address.
network IPv6Network Gets the associated network.
version int Gets the IP version number.
prefixlen int Gets the interface prefix length.
with_prefixlen str Gets the interface in address/prefix notation.
with_netmask str Gets the interface in address/netmask notation.

__str__() -> str

repr() uses the same method. Returns the address/prefix string form of the interface.