Why do most Python developers reach for the third-party `requests` library instead of the standard library's `urllib`?
Answer
`urllib.request` is part of the standard library and works without installing anything, but its API is verbose and low-level: building a request, handling redirects, decoding JSON, and managing cookies each require several explicit steps. `requests` wraps the same underlying capability in a much higher-level, ergonomic API — `requests.get(url).json()` — that handles redirects, encoding, and cookies sensibly by default. A single top-level call like `requests.get(url)` still opens its own short-lived connection; it is specifically `requests.Session()` that keeps a connection pool alive and reuses TCP/TLS connections and shared configuration like headers and cookies across multiple calls to the same host. Either way, `requests` became the de facto standard for HTTP calls in Python despite not being in the standard library.
Strong answer includes
- urllib is standard library but verbose and low-level
- requests wraps the same capability in a much higher-level ergonomic API
- a single requests.get() call opens its own connection; only Session() reuses connections and config across calls
Practical example
import requests
with requests.Session() as session:
session.headers.update({"Accept": "application/json"})
response = session.get(
"https://api.example.test/users/42",
timeout=5,
)
response.raise_for_status()
user = response.json()
print(user["id"])Session reuses connection pooling plus shared headers/cookies across calls. A timeout and raise_for_status() make failure behavior explicit instead of allowing indefinite waits or silently accepting error responses.
Expected result: For a successful JSON response such as {'id': 42}, it prints 42; network/status errors raise exceptions.