IP addressing, CIDR, and subnets
Read a prefix length, count addresses and usable hosts, and split a network into subnets.
import ipaddress
network = ipaddress.ip_network("192.168.10.0/26")
print(network.netmask, network.num_addresses)
hosts = list(network.hosts())
print(hosts[0], hosts[-1], len(hosts))
print(ipaddress.ip_address("192.168.10.70") in network)
subnets = list(ipaddress.ip_network("192.168.10.0/24").subnets(new_prefix=26))
print([str(subnet) for subnet in subnets])255.255.255.192 64 192.168.10.1 192.168.10.62 62 False ['192.168.10.0/26', '192.168.10.64/26', '192.168.10.128/26', '192.168.10.192/26']
Explanation
An IPv4 address is a 32-bit number written as four decimal bytes. CIDR (classless inter-domain routing) notation such as 192.168.10.0/26 gives a network address and a prefix length: the first 26 bits identify the network and the remaining 6 bits identify a host inside it. The netmask is the same idea as a bit pattern: 26 ones followed by 6 zeros is 255.255.255.192.
A /26 network has 2⁶ = 64 addresses. In ordinary IPv4 subnets the first address is the network address and the last is the broadcast address, so 62 can be assigned to hosts. Subnetting splits one network into smaller ones by borrowing host bits: a /24 divides into four /26 networks. Some ranges are reserved for private use (RFC 1918): 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16. IPv6 uses 128-bit addresses and a similar prefix notation, but no broadcast.
Python's ipaddress module does this arithmetic. The hosts() method returns the usable addresses and excludes the network and broadcast addresses for these ordinary subnets. Special prefixes such as /31 (point-to-point links, RFC 3021) and /32 do not follow that rule.
Explore the dry run
import ipaddress network = ipaddress.ip_network("192.168.10.0/26")print(network.netmask, network.num_addresses)hosts = list(network.hosts())print(hosts[0], hosts[-1], len(hosts))print(ipaddress.ip_address("192.168.10.70") in network)subnets = list(ipaddress.ip_network("192.168.10.0/24").subnets(new_prefix=26))print([str(subnet) for subnet in subnets])Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| The /26 prefix leaves 6 host bits, so the network spans 2⁶ = 64 addresses. | netmask 255.255.255.192 | — | |
| 26 one-bits then 6 zero-bits is 255.255.255.192; 64 addresses in total. | netmask 255.255.255.192 | 255.255.255.192 64 | |
| The usable hosts run from .1 to .62; .0 is the network address and .63 the broadcast address. | netmask 255.255.255.192 | 255.255.255.192 64 192.168.10.1 192.168.10.62 62 | |
| .70 is outside 192.168.10.0–.63, so it belongs to the next /26 block. | netmask 255.255.255.192 | 255.255.255.192 64 192.168.10.1 192.168.10.62 62 False | |
| Borrowing two host bits splits the /24 into four /26 subnets of 64 addresses each. | netmask 255.255.255.192 | 255.255.255.192 64 192.168.10.1 192.168.10.62 62 False ['192.168.10.0/26', '192.168.10.64/26', '192.168.10.128/26', '192.168.10.192/26'] |
Cost and common mistakes
Membership testing with the ipaddress module is O(1) arithmetic. Listing hosts or subnets is O(k) for k results.
- Usable hosts are addresses minus 2 only for ordinary subnets. /31 and /32 are special cases.
- ip_network() raises an error if host bits are set (for example 192.168.10.5/26) unless you pass strict=False.
- Private ranges are not routable on the public Internet. Devices using them need NAT to reach it.
Try it yourself
How many usable host addresses does a /28 IPv4 network have?
Reveal answer and reasoning
A /28 leaves 4 host bits, so 16 addresses and 14 usable hosts after removing the network and broadcast addresses.