Skip to content
KALANBLON
Show contents

Chapter 1 of 7 · 20 min

IP addresses

By the end of this chapter, you'll be able to

  • Explain the role of an IP address and read an IPv4 or IPv6 address
  • Use a mask or CIDR notation to delimit a network
  • Determine whether two machines are on the same local network
  • Tell private and public addresses apart, and understand the role of the home router (NAT)

An address for every machine

A network is a set of machines that can exchange data: computers, phones, printers, servers. The Internet is a network of networks. Every connected machine has an IP address (Internet Protocol).

  • An IPv4 address is 32 bits long, written as 4 numbers from 0 to 255: 192.168.1.24. There are only about 4.3 billion of them, fewer than the number of connected devices.
  • An IPv6 address is 128 bits long, written in hexadecimal: 2001:db8::8a2e:370:7334. The supply is practically inexhaustible. Both versions coexist today.

Python's ipaddress module can read and handle these addresses:

import ipaddress

ip = ipaddress.ip_address("192.168.1.24")
print(ip.version, int(ip))
print(format(int(ip), "032b"))
v6 = ipaddress.ip_address("2001:db8::8a2e:370:7334")
print(v6.version, v6.exploded)
4 3232235800
11000000101010000000000100011000
6 2001:0db8:0000:0000:0000:8a2e:0370:7334

An IPv4 address is just a 32-bit number, shown byte by byte to be readable. In IPv6, :: abbreviates a run of zero groups; exploded shows the full address.

Network and machine: the mask

An IP address has two parts: the network (like a street name) and the machine in that network (the house number). The mask says where the boundary lies. It is often written in CIDR notation: 192.168.1.0/24 means that the first 24 bits identify the network, and the last 8 the machine.

import ipaddress

network = ipaddress.ip_network("192.168.1.0/24")
print(network.netmask, network.num_addresses)
print(network.network_address, network.broadcast_address)
hosts = list(network.hosts())
print(len(hosts), hosts[0], hosts[-1])
print(ipaddress.ip_address("192.168.1.24") in network, ipaddress.ip_address("192.168.2.5") in network)
255.255.255.0 256
192.168.1.0 192.168.1.255
254 192.168.1.1 192.168.1.254
True False
  • /24 corresponds to the mask 255.255.255.0: 24 bits set to 1;
  • 8 bits for machines give 256 addresses, but the first one identifies the network itself and the last one is used for broadcast (a message for every machine): 254 usable addresses remain;
  • two machines on the same network talk directly. To reach another address, they go through the gateway (the router, often the home router), which forwards messages.

The message reaches the server, usually in a few tens of milliseconds. (203.0.113.x is a range reserved for documentation examples.)

Step 5 of 5
Text description of the animation
  1. Your computer wants to reach a server.
  2. It compares: the destination is not in its local network.
  3. So it hands the message to the gateway, whose address it knows.
  4. Each router chooses the next hop towards the destination network.
  5. The message reaches the server, usually in a few tens of milliseconds. (203.0.113.x is a range reserved for documentation examples.)

Private and public addresses

Some ranges are reserved for private networks (homes, companies): 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. They cannot be reached directly from the Internet, and millions of households reuse the same ones. The home router bridges the gap: every machine in the house goes out to the Internet with its public address. This is network address translation (NAT), which let IPv4 hold out despite its shortage.

import ipaddress

for text in ("192.168.1.24", "10.4.0.7", "172.20.3.1", "8.8.8.8", "127.0.0.1"):
    ip = ipaddress.ip_address(text)
    print(f"{text:<14} private: {ip.is_private!s:<5} loopback: {ip.is_loopback}")
192.168.1.24   private: True  loopback: False
10.4.0.7       private: True  loopback: False
172.20.3.1     private: True  loopback: False
8.8.8.8        private: False loopback: False
127.0.0.1      private: True  loopback: True

127.0.0.1 (localhost) means the machine itself: it is the address a developer uses to test a website on their own computer. Python also classes it as private, since it can never be reached from the Internet.

Mini-lab: split a networkPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: the neighbourhood check

Write a function same_network(a, b) that receives two interfaces in the format "address/prefix" and says whether the two machines can talk directly: each must see the other in its own network. Test it on these three pairs:

  • "192.168.1.10/24" and "192.168.1.200/24";
  • "192.168.1.10/24" and "192.168.0.5/24";
  • "10.0.5.1/16" and "10.0.200.9/24".
Hint 1
ipaddress.ip_interface("192.168.1.10/24") gives both .ip (the address) and .network (the network).
Hint 2
Test b.ip in a.network and a.ip in b.network.
Show the solution and common mistakes
import ipaddress

def same_network(a, b):
    a, b = ipaddress.ip_interface(a), ipaddress.ip_interface(b)
    return b.ip in a.network and a.ip in b.network

print(same_network("192.168.1.10/24", "192.168.1.200/24"))
print(same_network("192.168.1.10/24", "192.168.0.5/24"))
print(same_network("10.0.5.1/16", "10.0.200.9/24"))
True
False
False

The third case is tricky: the first machine (/16) does see the second in its network, but the second (/24, network 10.0.200.0/24) does not see the first. It will send its replies to the gateway: communication is asymmetric, and often broken. Inconsistent masks on the same local network are a classic source of failures.

Common mistakes:

  • Testing only one direction: you would wrongly conclude that the machines see each other in the third case.
  • Comparing addresses as text ("192.168.1" in a): 192.168.10.5 also starts with 192.168.1, and yet it is not in 192.168.1.0/24.

Summary

  1. 1An IP address identifies a machine: 32 bits in IPv4 (192.168.1.24), 128 bits in IPv6.
  2. 2The mask, or CIDR notation (/24), separates the network part from the machine part.
  3. 3Two machines on the same network talk directly; otherwise, they go through the gateway.
  4. 4Private addresses (10.x, 172.16-31.x, 192.168.x) are reused everywhere; the home router translates them (NAT).
  5. 5Inconsistent masks between two neighbouring machines are enough to break communication.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.