Part 04 / Stage 1
HTTP, networks and the shell
Goal: Read HTTP traffic fluently and debug connectivity methodically — half of on-site work is finding out why a request fails inside the customer's network. Time: ~3 hours · Needs: Python 3.14+, the course venv
What you will learn
- HTTP in practice: methods, status codes, headers, bodies
- Authentication schemes: API keys, bearer tokens, OAuth 2.0 client credentials
- DNS, TLS and certificates: what breaks and how to tell
- Proxies and firewalls: why requests fail inside corporate networks
- Shell essentials: curl, grep, jq, environment variables, ssh
- Reading errors methodically
Topics in this part
Practice
Work in the course venv. Check each answer by running it; compare with solutions/ only after trying.
Exercise 1: A retry decision function
Write should_retry(status) returning True for 429, 502, 503, 504 (and 408), False otherwise. Print its verdict for 200, 400, 401, 408, 429, 500, 503.
Show the solution to Exercise 1
# Exercise 1: which statuses to retry
RETRYABLE = {408, 429, 502, 503, 504}
def should_retry(status):
return status in RETRYABLE
for code in [200, 400, 401, 408, 429, 500, 503]:
print(code, "retry" if should_retry(code) else "do not retry")
Exercise 2: Build requests, never paste
Using httpx.Request, build a GET to https://api.acme.example/v1/shipments with params {"status": "in transit", "customer": "Kite Foods"}. Print the full URL and show that the space became + (or %20) without you doing anything.
Show the solution to Exercise 2
# Exercise 2: the library encodes the query for you
import httpx
req = httpx.Request("GET", "https://api.acme.example/v1/shipments",
params={"status": "in transit", "customer": "Kite Foods"})
print(req.url)
print("encoded space:", "in+transit" in str(req.url) or "in%20transit" in str(req.url))
Exercise 3: Classify failure layers
Write diagnose(error_text) that maps an error message to its layer: "Name or service not known" → dns, "Connection refused"/"timed out" → tcp, "certificate verify failed" → tls, anything starting with a 3-digit code → http, else unknown. Test it on five sample messages.
Show the solution to Exercise 3
# Exercise 3: map an error message to its failure layer
import re
def diagnose(error_text):
t = error_text.lower()
if "name or service not known" in t or "nodename nor servname" in t:
return "dns"
if "connection refused" in t or "timed out" in t:
return "tcp"
if "certificate verify failed" in t:
return "tls"
if re.match(r"\d{3} ", error_text):
return "http"
return "unknown"
samples = [
"Name or service not known",
"Connection refused",
"certificate verify failed: unable to get local issuer certificate",
"503 Service Unavailable",
"something strange happened",
]
for s in samples:
print(f"{diagnose(s):7s} <- {s}")
Exercise 4: Proxy environment check
Write a function proxy_report() that reads HTTPS_PROXY and NO_PROXY from the environment (via urllib.request.getproxies()) and prints a one-line summary. Demonstrate it twice: once with no proxy configured, once after setting both variables in os.environ.
Show the solution to Exercise 4
# Exercise 4: report the proxy environment
import os
import urllib.request
def proxy_report():
proxies = urllib.request.getproxies()
if not proxies:
print("no proxy configured - direct outbound connections")
else:
print(f"https proxy: {proxies.get('https')} | bypass: {proxies.get('no')}")
for key in [k for k in os.environ if k.lower() in ("http_proxy", "https_proxy", "no_proxy", "all_proxy")]:
del os.environ[key]
proxy_report()
os.environ.update({"HTTPS_PROXY": "http://proxy.acme.internal:3128", "NO_PROXY": "localhost,.acme.internal"})
proxy_report()
Mini-project: Connectivity checklist
A script the customer's IT team can run: it checks, in order, (1) DNS resolution for a configurable host, (2) TCP connect to a port, (3) proxy settings present/absent, and (4) an authenticated request against the simulated Acme API (fdelab.customer_api_transport()), printing PASS/FAIL with a clear message per step. Use localhost + the mock transport so it runs offline.
A solution is in solutions/mini_connectivity_checklist.py.
Stretch: make it a single acme-check command and test it on a network with a real proxy.
Common mistakes
| Mistake | Why it hurts | Fix |
|---|---|---|
| Retrying 400/401/403 | The request is wrong; retries hammer and never succeed | Retry only 429 and 5xx |
| Turning TLS verification off | "Works" today, silently insecure forever | Point the client at the customer's CA bundle |
| Hard-coding proxy settings | Breaks at the next site (or at this one next month) | Read HTTPS_PROXY/NO_PROXY from the environment |
| Pasting values into URLs by hand | &, spaces and unicode corrupt the query |
Pass params= and let the library encode |
| Debugging inside the full pipeline | Too many variables | One curl first; isolate the layer |
Quick recap
- The first digit of a status code tells you whose problem it is, and only 429 and 5xx responses are worth retrying.
- A 401 means fix your credentials and a 403 means ask for permission; in the OAuth client-credentials flow, a secret is exchanged for short-lived tokens.
- Failures happen in layers — DNS, then TCP, then TLS, then HTTP — and each layer fails with its own signature error.
- Corporate proxies and private certificate authorities are normal; configure them, and never disable verification.
- curl, jq, grep and ssh cover most on-site debugging.
- Debug methodically: reproduce the failure small, isolate the layer, and compare against a known-good request.
Checklist
- I can classify any status code's first digit and the two retryable families.
- I can explain bearer tokens and OAuth client credentials in two sentences.
- I can name the four failure layers and one signature error for each.
- I know the two proxy environment variables and who reads them.
- I reproduced a request with curl and inspected JSON with jq.
Read more: MDN: HTTP status codes · httpx docs