On this page
ipaddress
Prev: io | Up: stdlib_module | Next: itertools
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — ipaddress
Tags: #public #python3 #stdlib
Import
import ipaddress
Overview
Type-safe IPv4 and IPv6 address, subnet/CIDR network, and host-interface objects (ip_address, ip_network, ip_interface). Reach for ipaddress for CIDR membership checks, SSRF allow/blocklists (is_private, is_loopback, is_global), and subnet splitting instead of error-prone string splitting.
Quick Start
import ipaddress
addr = ipaddress.ip_address("192.168.1.42")
net = ipaddress.ip_network("192.168.1.0/24")
assert addr in net
assert addr.is_private and not addr.is_global
assert net.num_addresses == 256
Implementation Walkthrough
1. Guarding Against SSRF (Blocking Private / Loopback / Link-Local IPs)
import ipaddress
def is_public_ip(raw: str) -> bool:
try:
ip = ipaddress.ip_address(raw)
except ValueError:
return False
return ip.is_global and not (ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_multicast)
assert is_public_ip("93.184.216.34")
assert not is_public_ip("127.0.0.1")
assert not is_public_ip("169.254.169.254")
2. Subnetting & Iterating Usable Hosts
import ipaddress
net = ipaddress.ip_network("10.0.0.0/24")
halves = list(net.subnets(new_prefix=25))
assert [str(s) for s in halves] == ["10.0.0.0/25", "10.0.0.128/25"]
Gotchas & Boundaries
- Pass
strict=Falsetoip_network("192.168.1.5/24", strict=False)if the input has host bits set (otherwiseValueErroris raised). - IPv4 and IPv6 objects cannot be compared with
<or>: MixingIPv4AddressandIPv6AddressraisesTypeError. - Don't materialize
list(net.hosts())on large IPv6 networks: A/64has $2^{64}$ addresses—iterate lazily or use membership checks (addr in net).
Common Functions
- ipaddress.collapse_addresses() —
collapse_addresses(addresses)— Collapse a list of IP objects. - ipaddress.get_mixed_type_key() —
get_mixed_type_key(obj)— Return a key suitable for sorting between networks and addresses. - ipaddress.ip_address() —
ip_address(address)— Take an IP string/int and return an object of the correct type. - ipaddress.ip_interface() —
ip_interface(address)— Take an IP string/int and return an object of the correct type. - ipaddress.ip_network() —
ip_network(address, strict=True)— Take an IP string/int and return an object of the correct type. - ipaddress.summarize_address_range() —
summarize_address_range(first, last)— Summarize a network range given the first and last IP addresses. - ipaddress.v4_int_to_packed() —
v4_int_to_packed(address)— Represent an address as 4 packed bytes in network (big-endian) order. - ipaddress.v6_int_to_packed() —
v6_int_to_packed(address)— Represent an address as 16 packed bytes in network (big-endian) order.
Classes & Objects
- ipaddress.AddressValueError — A Value Error related to the address. (Methods:
.add_note(),.with_traceback()) - ipaddress.IPv4Address — Represent and manipulate single IPv4 Addresses. (Methods:
.compressed(),.exploded(),.ipv6_mapped(),.is_global(),.is_link_local(),.is_loopback()) - ipaddress.IPv4Interface —
ipaddress.IPv4Interfaceclass. (Methods:.compressed(),.exploded(),.ip(),.ipv6_mapped(),.is_global(),.is_link_local()) - ipaddress.IPv4Network — This class represents and manipulates 32-bit IPv4 network + addresses.. (Methods:
.address_exclude(),.compare_networks(),.compressed(),.exploded(),.hosts(),.is_global()) - ipaddress.IPv6Address — Represent and manipulate single IPv6 Addresses. (Methods:
.compressed(),.exploded(),.ipv4_mapped(),.is_global(),.is_link_local(),.is_loopback()) - ipaddress.IPv6Interface —
ipaddress.IPv6Interfaceclass. (Methods:.compressed(),.exploded(),.ip(),.ipv4_mapped(),.is_global(),.is_link_local()) - ipaddress.IPv6Network — This class represents and manipulates 128-bit IPv6 networks. (Methods:
.address_exclude(),.compare_networks(),.compressed(),.exploded(),.hosts(),.is_global()) - ipaddress.NetmaskValueError — A Value Error related to the netmask. (Methods:
.add_note(),.with_traceback())
Constants & Attributes
ipaddress.IPV4LENGTH(int) =32ipaddress.IPV6LENGTH(int) =128
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/io
- tuts/python3/stdlib_module/ipaddress/AddressValueError
- tuts/python3/stdlib_module/ipaddress/collapse_addresses
- tuts/python3/stdlib_module/ipaddress/get_mixed_type_key
- tuts/python3/stdlib_module/ipaddress/ip_address
- tuts/python3/stdlib_module/ipaddress/ip_interface
- tuts/python3/stdlib_module/ipaddress/ip_network
- tuts/python3/stdlib_module/ipaddress/IPv4Address
- tuts/python3/stdlib_module/ipaddress/IPv4Interface
- tuts/python3/stdlib_module/ipaddress/IPv4Network
- tuts/python3/stdlib_module/ipaddress/IPv6Address
- tuts/python3/stdlib_module/ipaddress/IPv6Interface
- tuts/python3/stdlib_module/ipaddress/IPv6Network
- tuts/python3/stdlib_module/ipaddress/NetmaskValueError
- tuts/python3/stdlib_module/ipaddress/summarize_address_range
- tuts/python3/stdlib_module/ipaddress/v4_int_to_packed
- tuts/python3/stdlib_module/ipaddress/v6_int_to_packed
- tuts/python3/stdlib_module/itertools
- tuts/python3/stdlib_module/socket/inet_aton
- tuts/python3/stdlib_module/socket/inet_pton
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/io Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ipaddress/AddressValueError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ipaddress/collapse_addresses Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ipaddress/get_mixed_type_key Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ipaddress/ip_address Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ipaddress/ip_interface Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ipaddress/ip_network Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ipaddress/IPv4Address Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ipaddress/IPv4Interface Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ipaddress/IPv4Network Linked here; links to this page; shared tags: #stdlib