Skip to main content

sessions for aiohttp.web

Project description

aiohttp_session

The library provides sessions for aiohttp.web.

Usage

The library allows to store user-specific data into session object.

The session object has dict-like interface (operations like session[key] = value, value = session[key] etc. are present).

Before processing session in web-handler you have to register session middleware in aiohttp.web.Application.

A trivial usage example:

import asyncio
import time
from aiohttp import web
from aiohttp_session import get_session, session_middleware
from aiohttp_session.cookie_storage import EncryptedCookieStorage

@asyncio.coroutine
def handler(request):
    session = yield from get_session(request)
    session['last_visit'] = time.time()
    return web.Response(body=b'OK')

@asyncio.coroutine
def init(loop):
    app = web.Application(middlewares=[session_middleware(
        EncryptedCookieStorage(b'Sixteen byte key'))])
    app.router.add_route('GET', '/', handler)
    srv = yield from loop.create_server(
        app.make_handler(), '0.0.0.0', 8080)
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

All storages uses HTTP Cookie named AIOHTTP_COOKIE_SESSION for storing data.

Available session storages are:

  • aiohttp_session.SimpleCookieStorage() – keeps session data as plain JSON string in cookie body. Use the storage only for testing purposes, it’s very non-secure.

  • aiohttp_session.cookie_storage.EncryptedCookieStorage(secret_key) – stores session data into cookies as SimpleCookieStorage but encodes it via AES cipher. secrect_key is a bytes key for AES encryption/decryption, the length should be 16 bytes.

    Requires crypotgraphy library:

    $ pip install aiohttp_session[secure]
  • aiohttp_session.redis_storage.RedisStorage(redis_pool) – stores JSON-ed data into redis, keepeng into cookie only redis key (random UUID). redis_pool is aioredis pool object, created by yield from aioredis.create_pool(...) call.

    Requires aioredis library:

    $ pip install aiohttp_session[aioredis]

License

aiohttp_session is offered under the Apache 2 license.

Changes

0.4.0 (2016-01-06)

  • Add optional NaCl encrypted storage #20

  • Relax EncryptedCookieStorage to accept base64 encoded string, e.g. generated by Fernet.generate_key.

  • Add setup() function

  • Save the session even on exception in the middleware chain

0.3.0 (2015-11-20)

  • Reflect aiohttp changes: minimum required Python version is 3.4.1

  • Use explicit ‘aiohttp_session’ package

0.2.0 (2015-09-07)

  • Add session.created property #14

  • Replaced PyCrypto with crypthography library #16

0.1.2 (2015-08-07)

  • Add manifest file #15

0.1.1 (2015-04-20)

  • Fix #7: stop cookie name growing each time session is saved

0.1.0 (2015-04-13)

  • First public release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp_session-0.4.0.tar.gz (91.0 kB view details)

Uploaded Source

Built Distribution

aiohttp_session-0.4.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_session-0.4.0.tar.gz.

File metadata

File hashes

Hashes for aiohttp_session-0.4.0.tar.gz
Algorithm Hash digest
SHA256 a47c521cc7cb8314786f5649c242e4b208522b4ce602a5e41617274a5fb2da70
MD5 6d18316eb50e46e212198f5432818d0f
BLAKE2b-256 880030588751ff04706a5594b5874d2e99f85a51892e575489c27a3555e10e6a

See more details on using hashes here.

File details

Details for the file aiohttp_session-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_session-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 38c00243a044eea52b3b1db27b9a5f8799f1c4b60ec6635ed0d88152450de4ff
MD5 d8d4e9c75dc197f25a7c208309bfdaac
BLAKE2b-256 08cb046a0ebedfc361866ef48772bd9f30e030cc85214e215fd5f0469a8f3ee5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page