← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:kfc2wpdx

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.},
}

Markdown

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.

All artifact files (JSON index)

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.