Python ipaddress module: subnetting, validation and IP planning in code
In short: Python's ipaddress module, part of the standard library, handles IP maths reliably. ip_address() validates single addresses, ip_network() gives network, broadcast, mask and hosts, the in operator checks membership, subnets() splits blocks and collapse_addresses() summarises routes. It works for IPv4 and IPv6 and avoids error-prone string handling.
Why use the ipaddress module?
Network scripts constantly deal with IP addresses: checking user input, finding which subnet a device belongs to, or planning new VLANs. Treating addresses as plain text leads to bugs, such as accepting "10.0.0.300". Python's ipaddress module understands addresses and networks properly and is built into Python, so it works everywhere. It is the code version of our subnet calculator.
Addresses and validation
import ipaddress
ip = ipaddress.ip_address("192.168.10.25")
print(ip.version) # 4
print(ip.is_private) # True
try:
ipaddress.ip_address("10.0.0.300")
except ValueError:
print("Not a valid IP address")
ip_address() raises a ValueError for anything invalid, which makes it ideal for checking input from users, spreadsheets or device output.
Network calculations and membership
net = ipaddress.ip_network("192.168.10.0/26")
print(net.network_address) # 192.168.10.0
print(net.broadcast_address) # 192.168.10.63
print(net.netmask) # 255.255.255.192
print(net.num_addresses - 2) # 62 usable hosts
hosts = list(net.hosts())
print(hosts[0], hosts[-1]) # 192.168.10.1 192.168.10.62
print(ipaddress.ip_address("192.168.10.40") in net) # True
If you pass an address with host bits set, such as 192.168.10.5/26, ip_network() raises an error. Use strict=False to get the network it belongs to, or ip_interface() to keep both the address and its network.
Subnetting and summarising
block = ipaddress.ip_network("10.20.0.0/22")
for sub in block.subnets(new_prefix=24):
print(sub)
# 10.20.0.0/24, 10.20.1.0/24, 10.20.2.0/24, 10.20.3.0/24
routes = [ipaddress.ip_network(n) for n in
["10.20.0.0/24", "10.20.1.0/24", "10.20.2.0/24", "10.20.3.0/24"]]
print(list(ipaddress.collapse_addresses(routes)))
# [IPv4Network('10.20.0.0/22')]
subnets() splits a block, and collapse_addresses() combines neighbouring networks into the smallest list, which is exactly route summarisation. The same functions work with IPv6, for example ip_network("2001:db8:10::/48").subnets(new_prefix=64).
Useful extras and common pitfalls
- Private, reserved and special addresses: properties such as
is_private,is_loopback,is_multicastandis_link_localhelp you filter addresses, for example to ignore loopbacks in an inventory. - Comparing and sorting: address and network objects can be compared and sorted properly, so 10.0.0.9 comes before 10.0.0.10, which plain text sorting gets wrong.
- Overlaps:
net_a.overlaps(net_b)tells you whether two subnets share any addresses, a quick way to catch planning mistakes. - Wildcard masks:
net.hostmaskgives the inverse mask used in Cisco ACLs and OSPF network statements, for example 0.0.0.63 for a /26. - Strings in, objects out: convert back with
str()when you need plain text for a template or a CSV file.
A common beginner mistake is forgetting that hosts() excludes the network and broadcast addresses, while iterating over the network itself includes them. For /31 and /32 networks, hosts() follows the modern rules for point-to-point links and single hosts. When in doubt, print the result and compare it with a calculator before relying on it in a script.
Real-world example: checking a VLAN plan
A fictional college network team keeps its VLAN plan in a spreadsheet. Before each change, a short Python script reads every subnet, uses ip_network() to reject typos, and compares each pair with overlaps() to find clashes. On its first run, it finds two VLANs that both used 10.50.8.0/23 and one gateway address outside its own subnet. Both mistakes had gone unnoticed for months. The script now runs automatically whenever the plan changes, alongside the team's source of truth.
Practice
Which function checks whether a string is a valid IP address?
How do you test whether an address belongs to a subnet?
Which function summarises a list of neighbouring networks?
Key takeaways
- The ipaddress module is built into Python and handles IP maths reliably.
- ip_address() validates addresses; ip_network() gives network, broadcast, mask and hosts.
- Use the in operator to check subnet membership.
- subnets() splits blocks and collapse_addresses() summarises routes.
- The same tools work for IPv6.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.