Calculate subnet boundaries without enumeration
Describe IPv4 and IPv6 networks using arithmetic instead of constructing huge address lists.
A network object gives you the range
ip_network provides a network address, prefix length and number of addresses. With strict=False, host bits are cleared: an input such as 192.0.2.17/24 describes 192.0.2.0/24. This is an explicit normalization choice. Allocation policy in the next lesson will use strict parsing instead.
An IPv6 network can contain far too many addresses to enumerate. Avoid list(network.hosts()) just to obtain a count or an endpoint. Integer address arithmetic and num_addresses answer those questions directly.
Document the host convention
Use the convention of Python's hosts() iterator. Ordinary IPv4 subnets exclude the network and broadcast addresses; /31 and /32 include every address. Ordinary IPv6 prefixes exclude the first address; /127 and /128 include every address. This is a calculation convention, not a promise that all addresses are available under a real organisation's allocation policy.
The library calls the upper endpoint broadcast_address for both object types. IPv6 does not use broadcast, so the returned report calls that field last_address. Names in a report should describe their meaning rather than accidentally imply a protocol behavior.
Return canonical text and numeric counts. Large integers remain exact in Python, making an IPv6 /64 just as straightforward to describe as an IPv4 /30 without consuming a huge amount of memory.
from ipaddress import ip_network
network = ip_network("192.0.2.17/24", strict=False)
print(network, network.num_addresses, network.network_address, network.broadcast_address)
Put it into practice.
Define subnet_details(cidr). Require an unscoped string containing /; use ip_network(..., strict=False) to clear host bits. Return network (canonical CIDR), version, prefixlen, last_address (upper endpoint), first_host, last_host, usable_hosts. Follow Python hosts() conventions: IPv4 prefixes below /31 exclude both endpoints; /31 and /32 include all. IPv6 prefixes below /127 exclude only the first address; /127 and /128 include all. Invalid input raises ValueError. Use arithmetic, not enumerated host lists.
See an explained solution
from ipaddress import ip_network
def subnet_details(cidr):
if not isinstance(cidr, str) or "%" in cidr or "/" not in cidr:
raise ValueError("Use an unscoped CIDR string")
network = ip_network(cidr.strip(), strict=False)
count = network.num_addresses
if network.version == 4 and network.prefixlen < 31:
first, last, usable = network.network_address + 1, network.broadcast_address - 1, count - 2
elif network.version == 6 and network.prefixlen < 127:
first, last, usable = network.network_address + 1, network.broadcast_address, count - 1
else:
first, last, usable = network.network_address, network.broadcast_address, count
return {"network":str(network), "version":network.version, "prefixlen":network.prefixlen,
"last_address":str(network.broadcast_address), "first_host":str(first), "last_host":str(last), "usable_hosts":usable}
if __name__ == "__main__":
print(subnet_details("192.0.2.17/24"))
The result uses exact counts and address arithmetic. Explicit edge cases preserve the library’s host convention while last_address avoids calling an IPv6 endpoint a broadcast.