ipaddress — IPv4/IPv6 addresses and networks

ipaddress strict networks, collapse_addresses, address comparison, and 3.13 is_private / is_global classification, using documentation-range addresses only.

模块页正文目前为英文。

The ipaddress module parses IPv4 and IPv6 addresses and networks. Import it as import ipaddress. The examples use only documentation and other reserved ranges such as 192.0.2.0/24 (TEST-NET-1), 2001:db8::/32, 10.0.0.0/8, 127.0.0.0/8, and 100.64.0.0/10. They do not connect to a host and they do not use public operator addresses as sample data.

On Python 3.13, is_private and is_global follow the IANA special-purpose registries, with the documented exception that 100.64.0.0/10 is neither private nor global. That is a different rule from earlier versions that treated is_private more like RFC 1918 alone, so these flags are version-specific. 192.0.2.1 is is_private True and is_global False on 3.13.12, even though it is a documentation address rather than an RFC 1918 private address.

Examples were run against Python 3.13.12. Links point to the Python 3.13 documentation.

Examples were checked against Python 3.13. See the official documentation for the complete API.

Python reference overview →

strict networks, collapse, and comparison

ip_network("192.0.2.1/24") raises ValueError because the host bits are set and strict defaults to True. Passing strict=False returns the network 192.0.2.0/24. collapse_addresses merges adjacent networks: 192.0.2.0/25 and 192.0.2.128/25 become 192.0.2.0/24.

Addresses of the same version compare by numeric value. Comparing an IPv4 address to an IPv6 address raises TypeError, as in 192.0.2.1 versus 2001:db8::1. That error is about the IP version, not about reachability.

ipaddress.ip_network, collapse_addresses, and address comparison

ipaddress.ip_address(address)
ipaddress.ip_network(address, strict=True)
ipaddress.collapse_addresses(addresses)

Reject a strict network with host bits set, collapse two halves of 192.0.2.0/24, and show same-version versus mixed-version comparison.

ParameterDescription
addressA string or integer address, or a network in CIDR form. The values here are documentation ranges only.
strictWhen True (the default), a network string must have host bits clear. When False, host bits are masked off.

Returns: ip_address returns an IPv4Address or IPv6Address. ip_network returns a network. collapse_addresses yields merged networks.

Raises

  • ValueError — Raised when a strict network has host bits set, or when the text is not a valid address or network.
  • TypeError — Raised when IPv4 and IPv6 addresses are compared to each other.
  • These calls only parse strings. They do not open sockets.
python
import ipaddress

try:
    ipaddress.ip_network("192.0.2.1/24")
except ValueError as exc:
    print(type(exc).__name__)
print(ipaddress.ip_network("192.0.2.1/24", strict=False))
print(list(ipaddress.collapse_addresses([
    ipaddress.ip_network("192.0.2.0/25"),
    ipaddress.ip_network("192.0.2.128/25"),
])))
print(ipaddress.ip_address("192.0.2.1") < ipaddress.ip_address("192.0.2.2"))
try:
    ipaddress.ip_address("192.0.2.1") < ipaddress.ip_address("2001:db8::1")
except TypeError as exc:
    print(type(exc).__name__)

Expected output

ValueError
192.0.2.0/24
[IPv4Network('192.0.2.0/24')]
True
TypeError

Official documentation →

is_private and is_global on Python 3.13

The printed triples are is_private, is_global, and is_loopback for each address, as measured on Python 3.13.12. 10.0.0.1 is private. 127.0.0.1 is private and loopback. 192.0.2.1 and 2001:db8::1 are not globally reachable according to the 3.13 IANA-based tables, so is_private is True. 100.64.0.1 is the documented exception where both is_private and is_global are False.

IPv4Address.is_private, is_global, and is_loopback

IPv4Address.is_private
IPv4Address.is_global
IPv4Address.is_loopback
IPv6Address.is_private
IPv6Address.is_global
IPv6Address.is_loopback

Print 3.13 classification flags for documentation, private, loopback, shared, and documentation IPv6 addresses.

Returns: Boolean properties. On 3.13, is_private is the opposite of is_global except for 100.64.0.0/10, where both are False.

  • These flags changed relative to earlier Python releases that did not fully follow the IANA special-purpose registries. Read them as Python 3.13 behaviour, not as a universal historical definition.
python
import ipaddress

for text in ("192.0.2.1", "10.0.0.1", "127.0.0.1", "100.64.0.1", "2001:db8::1"):
    ip = ipaddress.ip_address(text)
    print(text, ip.is_private, ip.is_global, ip.is_loopback)

Expected output

192.0.2.1 True False False
10.0.0.1 True False False
127.0.0.1 True False True
100.64.0.1 False False False
2001:db8::1 True False False

Related: Official ipaddress

Official documentation →