aiohttp-client-middlewares

Reusable client middlewares for aiohttp.

This package collects ready-to-use middlewares for aiohttp.ClientSession. Available middlewares:

Installation

$ pip install aiohttp-client-middlewares

Quickstart

Attach one or more middlewares to a session through the middlewares argument. HTTP Digest authentication:

digest_auth = DigestAuthMiddleware(login="user", password="secret")
async with ClientSession(middlewares=(digest_auth,)) as session:
    url = "https://httpbin.org/digest-auth/auth/user/secret"
    async with session.get(url) as resp:
        resp.raise_for_status()
        print(await resp.json())

Client-side rate limiting (when combined with other middlewares, list the limiter last so that internal replays, such as digest’s 401 handshake, are throttled too):

# At most 5 requests per second, allowing bursts of up to 2.
rate_limit = RateLimitMiddleware(TokenBucket(rate=5.0, burst=2))
async with ClientSession(middlewares=(rate_limit,)) as session:
    async with session.get("http://example.com") as resp:
        resp.raise_for_status()
        print(await resp.text())

Contents

Indices and tables