| name | aiohttp-session-2-12-0 |
| description | Server-side sessions for aiohttp.web applications using aiohttp-session 2.12, providing multiple storage backends including encrypted cookies (Fernet/NaCl), Redis, and Memcached for persistent user state management. Use when building aiohttp.web applications that require session-based authentication, shopping carts, user preferences, or any per-request user-specific data persistence. |
| license | MIT |
| author | Tangled <noreply@tangledgroup.com> |
| version | 0.1.0 |
| tags | ["aiohttp","sessions","web","async","storage","cookies","redis","memcached","encryption"] |
| category | development |
| external_references | ["https://github.com/aio-libs/aiohttp-session","https://aiohttp-session.readthedocs.io/"] |
aiohttp-session 2.12
Overview
aiohttp-session provides server-side session support for aiohttp.web applications. It gives every HTTP request access to a dict-like Session object that persists user-specific data across requests. The library supports multiple storage backends — from simple plaintext cookies (testing only) to Fernet-encrypted cookies, NaCl-encrypted cookies, Redis, and Memcached — letting you choose the right trade-off between security, performance, and infrastructure complexity.
The session is integrated as aiohttp middleware, automatically loading on request and saving on response. Session data is stored in an HTTP cookie named AIOHTTP_SESSION by default (configurable via cookie_name).
When to Use
- Building aiohttp.web applications that need per-user state across requests
- Implementing session-based authentication flows
- Storing shopping cart contents, user preferences, or form draft data
- Migrating from framework-provided sessions to a lightweight asyncio-compatible alternative
- Needing encrypted cookie sessions without server-side storage
- Requiring distributed session storage via Redis or Memcached for multi-instance deployments
Core Concepts
Session: A dict-like (MutableMapping) object representing user state valid for a period of continual activity. Retrieved via await get_session(request) — never instantiated directly.
Storage: The backend responsible for persisting and loading session data. All storages derive from AbstractStorage and implement load_session() and save_session(). Every storage uses an HTTP cookie to store at least the session key; some (cookie-based) store all data in the cookie itself.
Middleware: The session system works as aiohttp middleware, registered via setup(app, storage). It attaches the storage to each request and automatically saves changed sessions on response.
Session Lifecycle: On each request, the middleware loads the session from storage. If the handler modifies it (via __setitem__, del, or explicit session.changed()), the session is serialized and saved when the response is generated.
Installation / Setup
Install with pip:
pip install aiohttp-session
Optional extras for specific backends: