flowchart TD
A["requests.get('https://host/path')"] --> B["DNS: host -> IP address"]
B --> C["TCP connect to IP:443"]
C --> D["TLS handshake: certificate, cipher, keys"]
D --> E["write an HTTP request: method, path, headers"]
E --> F["read the status line and headers"]
F --> G["read the body -- length or chunked"]
G --> H["decode: gzip, then the charset"]
H --> I["a Response object"]
C -.->|"this layer is all socket.send / socket.recv gives you"| J["bytes in order, no structure"]
What is networking?
Networking is how computers exchange data.
Key concepts:
- IP address: identifies a machine
- Port: identifies a program/service on that machine
- Protocol: rules for communication
Common protocols:
- TCP: reliable (web, SSH)
- UDP: fast, not guaranteed (streaming, DNS)
- HTTP/HTTPS: web protocol (built on TCP)
What you’ll learn here
- Make HTTP requests (APIs) using
requestsandurllib - Work with JSON data
- Build a basic HTTP server
- Use sockets for low-level TCP communication
- Timeouts, retries, and error handling
Safety note
Always be careful when handling:
- user input from the internet
- authentication tokens
- opening ports on public machines
How this section is organised
This page is the map for the networking lessons. Work through them in this order and each one builds on the last:
- Requests and APIs: fetch data over HTTP with
urllibandrequests, then read the JSON that comes back. - Building a Simple HTTP Server: the other side of the conversation, serving responses with
http.server. - Sockets: the low-level TCP layer that HTTP itself is built on.
- Networking Errors and Timeouts: what to do when the network misbehaves.
What happens when you open a web page
Seeing the layers together makes everything later easier. When your code calls requests.get("https://example.com/data"), these steps happen in order:
- DNS turns the name
example.cominto an IP address. - A TCP connection is opened to that address on a port (443 for HTTPS, 80 for HTTP).
- For HTTPS, a TLS handshake encrypts the connection.
- Your program writes an HTTP request, and the server writes back a status, headers and a body.
Each step can fail on its own, which is why errors and timeouts get a lesson of their own.
IP, port and protocol in practice
An IP address finds the machine and a port finds the program on it, much like a building address and a flat number. 127.0.0.1 (also called localhost) always means "this computer", which makes it perfect for practising without any real network. Python's standard library can show you both:
import socket
# Ask the operating system for the address of the local machine name
print(socket.gethostbyname("localhost")) # 127.0.0.1
# Well-known ports
print(socket.getservbyname("http")) # 80
print(socket.getservbyname("https")) # 443Common misunderstandings
- "TCP sends messages." It sends a stream of bytes. Two sends may arrive as one read, or one send as two reads, so protocols must mark where messages end.
- "UDP is just a worse TCP." It is deliberately lightweight: no connection and no delivery guarantee, which suits live video, games and DNS lookups.
- "HTTPS is a different network." It is HTTP carried inside TLS over the same TCP.
Where to start
If you only want to consume an API, go straight to requests and JSON. If you want to understand how it works underneath, continue with sockets after the HTTP server lesson.
Check yourself
What does a TCP socket actually guarantee?
Measured elsewhere on this site: `sendall(b'HELLO')` then `sendall(b'WORLD')` read by `recv(4)` came back as `[b'HELL', b'O', b'WORL']`. Framing is the application's job.
Show answer
B — Bytes arrive in order and intact, with no message boundaries — Measured elsewhere on this site: `sendall(b'HELLO')` then `sendall(b'WORLD')` read by `recv(4)` came back as `[b'HELL', b'O', b'WORL']`. Framing is the application's job.
Which of these does a raw socket NOT do for you?
Ordering, retransmission and checksums are TCP's job. Everything protocol-shaped above that — HTTP framing, TLS, content decoding — is written on top.
Show answer
C — Parse HTTP headers — Ordering, retransmission and checksums are TCP's job. Everything protocol-shaped above that — HTTP framing, TLS, content decoding — is written on top.
What is the first thing `requests.get('https://host/path')` must do?
You cannot connect to a name. DNS resolution happens first, then the TCP connection, then the TLS handshake, and only then does any HTTP get written.
Show answer
B — Resolve the hostname to an IP address — You cannot connect to a name. DNS resolution happens first, then the TCP connection, then the TLS handshake, and only then does any HTTP get written.
Why use a `Session` for repeated calls to the same host?
Reusing the connection skips the TCP and TLS handshakes on every subsequent request, which is usually the dominant cost for small responses.
Show answer
B — It reuses the connection and shares headers and cookies — Reusing the connection skips the TCP and TLS handshakes on every subsequent request, which is usually the dominant cost for small responses.
🧪 Try It Yourself
Exercise 1 – HTTP GET with urllib
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Socket Connection
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Resolve a Hostname
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.