Networking in Python
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?
Section titled “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
Section titled “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
Section titled “Safety note”Always be careful when handling:
- user input from the internet
- authentication tokens
- opening ports on public machines
Check yourself
Section titled “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.
pch.quizShowAnswer
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.
pch.quizShowAnswer
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.
pch.quizShowAnswer
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.
pch.quizShowAnswer
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
Section titled “🧪 Try It Yourself”Exercise 1 – HTTP GET with urllib
Section titled “Exercise 1 – HTTP GET with urllib”Exercise 2 – Socket Connection
Section titled “Exercise 2 – Socket Connection”Exercise 3 – Resolve a Hostname
Section titled “Exercise 3 – Resolve a Hostname”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading