8. Files, Networking, and HTTP APIs
Build reliable networked code: set timeouts, stream data, and manage resources deterministically.
Question: What are the key components of a modern Python web stack?
Answer: A modern stack is built around the ASGI standard. This typically involves a high-performance ASGI framework like FastAPI or Starlette, paired with an ASGI server like Uvicorn or Hypercorn. For HTTP clients, libraries like httpx
or aiohttp
are standard choices as they support asyncio
.
Explanation: The ASGI (Asynchronous Server Gateway Interface) standard allows for a separation between the web framework and the web server, enabling high-performance asynchronous applications that can handle long-lived connections like WebSockets, which was not possible with the older WSGI standard.
from fastapi import FastAPI
app = FastAPI()
@app.get("/health")
def health():
return {"status": "ok"}
Question: How do you set safe timeouts, retries, and connection pooling with an HTTP client?
Answer: Always set connect/read/write timeouts, reuse connections via a session/client, and implement application-level retries for transient errors.
Explanation: Many clients don’t retry by default; ensure idempotency before retrying.
import httpx, asyncio
timeout = httpx.Timeout(connect=2.0, read=5.0, write=5.0)
limits = httpx.Limits(max_connections=100, max_keepalive_connections=20)
async def get_json(url: str):
async with httpx.AsyncClient(timeout=timeout, limits=limits) as c:
for _ in range(3):
try:
r = await c.get(url)
r.raise_for_status(); return r.json()
except (httpx.ReadTimeout, httpx.ConnectError):
await asyncio.sleep(0.1)
Question: How do you stream large downloads/uploads safely?
Answer: Use streaming responses and iterate in chunks to limit memory usage.
Explanation: Avoid loading entire payloads into memory.
async with httpx.AsyncClient() as c:
async with c.stream("GET", url) as r:
async for chunk in r.aiter_bytes():
process(chunk)