awesome-python/website/data/category_intros/http-clients.md
Vinta Chen 06c39e9941
docs: add HTTP Clients category intro
Its meta description fell back to generic text with no guidance on which library to pick.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-09-27 07:34:16 +08:00

4.5 KiB

Offering one Requests-style API for sync and async code, HTTPX spares you a second Python HTTP client. Requests suits sync scripts, aiohttp busy asyncio apps.

How to choose:

  • Sync and async code from one API, or HTTP/2: HTTPX
  • Sync code with the simplest API: Requests
  • An asyncio app with many concurrent requests, WebSockets, or its own HTTP server: aiohttp
  • Connection pools and retries in your own hands, one layer below Requests: urllib3
  • HTTPX's API, verifying TLS with your OS's certificates: HTTPX2
  • Building and editing URLs: yarl

HTTPX has a broadly Requests-compatible API, sync by default with async when you need it, and runs on asyncio or Trio. Coming from Requests, httpx.Client takes the place of requests.Session. HTTP/2 is opt-in: install httpx[http2] and pass http2=True to the client, which pays off most when you send lots of concurrent async requests.

Python's own docs recommend Requests for a higher-level HTTP client. It's sync only, and its docs point to HTTPX, among others, for async.

aiohttp is an async client and server for asyncio, with WebSockets on both sides and middleware for the client. Its API is wordier than Requests' by design, to make the most of non-blocking operations. Each async with or await gives the event loop a chance to switch to other work. Install aiohttp[speedups] to get aiodns for faster DNS resolving, which its docs highly recommend.

urllib3 is what gives Requests its connection pooling. Use it directly when you want the pool and the retry policy in your own code. It can retry idempotent requests on its own: set the policy once on the PoolManager to cover every request.

HTTPX2 has the same public API as HTTPX under a new name, so switching means renaming the dependency and the import. It verifies TLS with your operating system's trust store instead of a bundled certificate list. The two packages install side by side, but their objects don't mix: when a library takes a client, build it from the package that library uses.

yarl's URL is immutable: every change returns a new URL, and strings you pass in get percent-encoded for you. Build paths with / and queries with %. Its docs pick immutability so you can hand a URL to other code without it being changed under you. aiohttp's request methods take a yarl URL as well as a string.

Whichever client you pick, create one session object and reuse it, since it holds the connection pool: Requests' Session, HTTPX's Client or AsyncClient, aiohttp's ClientSession, urllib3's PoolManager. Don't create one per request; make one per application and pass it around. Keep the shortcut functions for one-off scripts: HTTPX's top-level API opens a new connection for every request, and urllib3.request() shares one global pool with your dependencies.

Set timeouts, too. Requests never times out unless you pass timeout, and its docs say nearly all production code should. urllib3 takes one on the PoolManager to cover every request, and HTTPX enforces timeouts by default.