Python ipaddress - validate IPs, check subnets and iterate hosts for IPv4 and IPv6
Answer: ip_address() returns an IPv4Address or IPv6Address object and raises ValueError if the string is not a valid address; ip_network() returns a network object. The module makes it straightforward to check whether two hosts are on the same subnet or iterate over all hosts in a subnet.
Examples
import ipaddress
addr = ipaddress.ip_address("192.168.1.130") # IPv4Address
v6 = ipaddress.ip_address("2001:db8::") # IPv6Address
net = ipaddress.ip_network("192.168.1.0/24")
addr in net # True
net.num_addresses # 256
list(ipaddress.ip_network("192.168.1.128/30").hosts())
ipaddress.ip_network("192.168.1.128/30").subnet_of(net) # True
addr.is_private # True
ipaddress.ip_network("192.168.1.5/24", strict=False) # 192.168.1.0/24
iface = ipaddress.ip_interface("192.168.1.5/24")
iface.network # IPv4Network('192.168.1.0/24')
def is_valid_ip(text):
try:
ipaddress.ip_address(text)
return True
except ValueError:
return False
Factory functions
| Function | Returns | |---|---| | ip_address(address) | an IPv4Address or IPv6Address object | | ip_network(address, strict=True) | an IPv4Network or IPv6Network object | | ip_interface(address) | an IPv4Interface or IPv6Interface: an address plus its network |
Integers less than 2**32 are considered to be IPv4 by default.
Network details
- With
strict=True, host bits set in the supplied address raiseValueError; withstrict=Falsethe host bits are masked out. hosts()returns an iterator over the usable hosts, which excludes the network address and the broadcast address (except for /31 networks).num_addressesis the total number of addresses;prefixlenis the prefix length in bits.subnet_of(other)andsupernet_of(other)compare networks.is_privateis true for addresses not globally reachable per the IANA special registries, but false for the shared space100.64.0.0/10.- Mixing IPv4 and IPv6 objects in network methods raises
TypeError.
See also
Claims
- id: C1 claim: Invalid addresses raise ValueError. source: S1 evidence: "A ValueError is raised if address does not represent a valid IPv4 or IPv6 address."
- id: C2 claim: strict=True rejects host bits set in a network. source: S1 evidence: "host bits are set in the supplied address, then ValueError is raised."
- id: C3 claim: Small integers are treated as IPv4 by default. source: S2 evidence: "integers less than 2**32 will be considered to be IPv4 by default."
Sources
- [S1] ipaddress - IPv4/IPv6 manipulation library. Python docs. https://docs.python.org/3/library/ipaddress.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/ipaddress.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/ipaddress.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, ipaddress - IPv4/IPv6 manipulation library; python/cpython (GitHub), Lib/ipaddress.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, ipaddress - IPv4/IPv6 manipulation library; python/cpython (GitHub), Lib/ipaddress.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python ipaddress - validate IPs, check subnets and iterate hosts for IPv4 and IPv6." iinventedlight Library, iil:note:kfc2wpdx, version of 2026-11-18. https://iinventedlight.com/library/notes/python-ipaddress/. License: CC-BY-4.0.
@misc{iil_note_kfc2wpdx,
author = {{iinventedlight}},
title = {{Python ipaddress - validate IPs, check subnets and iterate hosts for IPv4 and IPv6}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-ipaddress/},
note = {Permanent ID iil:note:kfc2wpdx. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-ipaddress.table.json (table; CC-BY-4.0)
- python-ipaddress.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-ipaddress.md.