Addresses in Python

The ipaddress module does subnet arithmetic correctly, including the small blocks most calculators get wrong. This set covers building a network from a host address, why a /24 holds 256 addresses but only 254 usable hosts, why a /31 reserves nothing, and how to pull one block out of a split network. The task reads a network, splits it, and asks you to paste the four lines.

4 questions 1 task Read the lesson

Questions

Each one is marked as you go, and the explanation arrives straight after, which is the part worth reading.

4 questions, marked as you go.

1 Why does ipaddress.ip_network("192.168.1.42/24") raise a ValueError?
2 A /24 holds 256 addresses but hosts() yields 254. Why the difference?
3 For ip_network("10.0.0.4/31"), how many addresses does hosts() yield?
4 net = ip_network("192.168.1.0/24"); subs = list(net.subnets(new_prefix=26)). What is subs[2]?

Tasks

These need your own device. Run the command in Termux, then paste what it printed. The marking looks at what the output means, so spacing, version numbers and ordering do not matter. If you have not installed anything yet, the lesson covers the setup.

Read a network and split it

This builds the network that a host address sits in, reads its mask, its broadcast address and its address count, counts the usable hosts, splits the block into four smaller ones, and lists the addresses of a two address point-to-point link. Paste all four lines.

Run this in Termux On your own device. Nothing on this page executes it. The command runs in your terminal, and this box only reads what you paste back.

python3 -c "import ipaddress; net = ipaddress.ip_network('192.168.1.42/24', strict=False); print(net, net.netmask, net.broadcast_address, net.num_addresses); print(len(list(net.hosts()))); print(len(list(net.subnets(new_prefix=26)))); print(list(ipaddress.ip_network('10.0.0.4/31').hosts()))"
Marking your own work Both halves of this are checked in your browser, with no server involved, so nothing stops you from pasting output you did not run. The tasks are written so that the easiest way to pass one is to actually do it and read the result, which is the whole intention.

All sets Read the lesson