The network layer delivers packets between any two computers on the internet, across many networks. Every packet carries an IP address for its sender and its destination.
IPv4 addresses
An IPv4 address is 32 bits. People write it as four bytes in decimal, separated by dots: 192.168.1.10 is
11000000.10101000.00000001.00001010
Each part is 0 to 255 (8 bits). To turn a dotted address into one number, multiply as you go: ((192 × 256 + 168) × 256 + 1) × 256 + 10.
def to_number(ip):
n = 0
for part in ip.split("."):
n = n * 256 + int(part)
return n
def to_binary(ip):
return ".".join(format(int(part), "08b") for part in ip.split("."))
for ip in ["192.168.1.10", "10.0.0.1"]:
print(ip, "=", to_binary(ip), "=", to_number(ip))Network part and host part
An address has two parts. The first bits name the network and the rest name the host (one device) inside it. How many bits belong to the network is written after a slash, the prefix length: in 192.168.1.10/24, the first 24 bits (192.168.1) are the network and the last 8 bits are the host. Every device on that network shares the first 24 bits. Routers only need to know where networks are, not every device: that keeps routing tables small.
Before 1993, the split was fixed by classes: A (first byte 0–127, /8), B (128–191, /16) and C (192–223, /24). Any prefix length is allowed today (CIDR, classless routing), but you will still meet the class names.
Special and private addresses
| Address | Meaning |
|---|---|
127.0.0.1 | this computer itself ("localhost") |
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 | private: used inside homes and organisations, never on the open internet |
255.255.255.255 | broadcast to everyone on this network |
0.0.0.0 | "no address yet", or the default route |
Most homes have one public address. The router uses NAT (network address translation) to share it: outgoing packets get the router's public address, and it remembers which inside device each conversation belongs to.
Python's standard library knows these rules:
import ipaddress
for text in ["192.168.1.10", "172.16.0.5", "8.8.8.8"]:
print(text, "private:", ipaddress.ip_address(text).is_private)
print("127.0.0.1 is this computer:", ipaddress.ip_address("127.0.0.1").is_loopback)
net = ipaddress.ip_network("192.168.1.0/24")
print(str(net), "has", net.num_addresses, "addresses")
print("::1 is IPv6 version", ipaddress.ip_address("::1").version)IPv6
32 bits give about 4.3 billion addresses, which ran out around 2011. IPv6 addresses have 128 bits, written as eight groups of hex, like 2001:db8::8a2e:370:7334 (:: stands for a run of zero groups). There are enough for every device ever made, and IPv6 already carries a large share of internet traffic.
Common mistakes
- Writing a part above 255:
192.168.1.256is not an address. - Thinking a private address can be reached from the internet. Only through NAT, from inside.
- Thinking classes still decide the prefix length. Today the prefix is whatever the network's owner chose.
Quick check
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.