What Is aiohttp? β Definition and How It Works
Learn what aiohttp provides, why sessions and connectors matter, how to bound concurrency, and where it fits among Python HTTP clients.
aiohttp is an asynchronous HTTP client and server library for Python, built on asyncio. As a client it makes many simultaneous requests practical from a single thread.
1. The Session Is Central
import aiohttp, asyncio
async def main():
timeout = aiohttp.ClientTimeout(total=30)
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.get("https://example.com") as response:
response.raise_for_status()
html = await response.text()
A session owns the connection pool, cookie jar, and default headers. Creating one per request destroys connection reuse and is the most common performance mistake with this library.
2. The Connector Controls Concurrency
connector = aiohttp.TCPConnector(limit=100, limit_per_host=8)
session = aiohttp.ClientSession(connector=connector)
limit bounds total simultaneous connections; limit_per_host bounds them to any single destination. The second is the politeness control, and it matters more than the first β one host should never absorb the whole pool.
3. Timeouts
ClientTimeout sets total, connect, and read limits separately. Without them a single unresponsive host holds a task indefinitely, and at scale a few such hosts quietly consume the whole pool.
Set a total timeout on the session and override it per request where a particular call legitimately takes longer.
4. Reading Responses
text()decodes using headers, with an override available.read()returns raw bytes for binary payloads.json()parses, and can be told to accept unusual content types.contentstreams the body in chunks, for downloads too large to hold in memory.
Note that aiohttp does not raise on 4xx or 5xx by default β call raise_for_status() or check explicitly, or an error page will be parsed as data.
5. Bounding Work in Flight
Connector limits bound connections; a semaphore bounds tasks. Both are worth having, because thousands of pending tasks consume memory even when only a few are connected.
semaphore = asyncio.Semaphore(20)
async with semaphore:
...
6. Compared With Other Python Clients
| Library | Model | Notes |
|---|---|---|
| requests | Synchronous | Simplest, no concurrency |
| httpx | Both | Sync and async in one API, HTTP/2 support |
| aiohttp | Asynchronous | Mature, also a server framework |
For a new asynchronous project, httpx offers a familiar interface and HTTP/2. aiohttp remains a solid choice, particularly where its server side is also used.
7. What It Does Not Do
It does not execute JavaScript, so client-rendered pages return the served shell. It also presents the TLS and header profile of a Python client, which is observable β relevant when a destination classifies clients.
8. Summary
aiohttp is an asyncio HTTP client and server library whose session owns connection pooling and cookies, and whose connector bounds concurrency globally and per host.
Reuse one session, set explicit timeouts, cap per-host connections, check status codes explicitly, and pair connector limits with a semaphore to bound pending work.