Lib/ipaddress.py
Source:
cpython 3.14 @ ab2d84fe1023/Lib/ipaddress.py
ipaddress (PEP 3144) provides immutable types for IPv4 and IPv6 addresses, networks, and interfaces. All parsing is pure Python with no C extension.
Map
| Lines | Symbol | Role |
|---|---|---|
| 1-100 | ip_address, ip_network, ip_interface | Factory functions; auto-detect v4/v6 |
| 101-400 | _BaseAddress, _BaseNetwork | Shared base classes; comparison, __contains__ |
| 401-750 | IPv4Address | 32-bit packed int; packed, exploded, is_private, is_loopback |
| 751-1000 | IPv4Network | CIDR network; network_address, broadcast_address, hosts(), subnets() |
| 1001-1300 | IPv4Interface | Address + network; ip, network, with_prefixlen |
| 1301-1700 | IPv6Address | 128-bit packed int; ipv4_mapped, sixtofour, teredo, is_link_local |
| 1701-2100 | IPv6Network | IPv6 CIDR; subnets(), supernet(), subnet_of() |
| 2101-2300 | IPv6Interface, _get_mixed_type_key, collapse_addresses, summarize_address_range | Utilities |
Reading
Address storage
Both IPv4Address and IPv6Address store the address as a Python int in self._ip. String parsing converts to the packed int representation.
# CPython: Lib/ipaddress.py:1158 IPv4Address.__init__
def __init__(self, address):
if isinstance(address, int):
self._check_int(address, 32)
self._ip = address
elif isinstance(address, bytes):
...
self._ip = int.from_bytes(address, 'big')
else:
addr_str = str(address)
...
self._ip = struct.unpack('!I', socket.inet_aton(addr_str))[0]