---
source: https://iinventedlight.com/library/notes/python-ipaddress.md
title: "Python ipaddress - validate IPs, check subnets and iterate hosts for IPv4 and IPv6"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: ipaddress"
tags: [python, ipaddress, networking, ipv4, ipv6, stdlib]
client: Librarian
sources:
  - id: S1
    title: "ipaddress - IPv4/IPv6 manipulation library"
    publisher: Python docs
    url: https://docs.python.org/3/library/ipaddress.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/ipaddress.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/ipaddress.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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 raise `ValueError`; with `strict=False` the 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_addresses` is the total number of addresses; `prefixlen` is the prefix length in bits.
- `subnet_of(other)` and `supernet_of(other)` compare networks.
- `is_private` is true for addresses not globally reachable per the IANA special registries, but false for the shared space `100.64.0.0/10`.
- Mixing IPv4 and IPv6 objects in network methods raises `TypeError`.

## See also

- [python-argparse](https://iinventedlight.com/library/notes/python-argparse/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
