aiohttp-middlewares

2.4.0 · active · verified Thu Apr 16

aiohttp-middlewares is a collection of useful middlewares for aiohttp applications, providing common web-development needs like error handling, CORS, timeout, and shielding view handlers. The library is currently at version 2.4.0 and maintains a regular monthly release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up an aiohttp application with `cors_middleware` to allow requests from specific origins and `error_middleware` for global exception handling.

from aiohttp import web
from aiohttp_middlewares import (
    cors_middleware,
    error_middleware,
)

async def handler(request):
    return web.Response(text="Hello, aiohttp-middlewares!")

app = web.Application(
    middlewares=(
        cors_middleware(origins=("http://localhost:8081", "http://127.0.0.1:8081")),
        error_middleware(),
    )
)

app.router.add_get('/', handler)

if __name__ == '__main__':
    web.run_app(app)

view raw JSON →