Provides server-backed sessions for Sanic using Redis, Memcache and more.
Project description
Sanic session management for humans
sanic_session
is session management extension for Sanic that integrates server-backed sessions with most convenient API.
sanic_session
provides a number of session interfaces for you to store a client's session data. The interfaces available right now are:
- Redis (supports both drivers
aioredis
andasyncio_redis
) - Memcache (via
aiomcache
) - Mongodb (via
sanic_motor
andpymongo
) - In-Memory (suitable for testing and development environments)
Installation
Install with pip
(there is other options for different drivers, check documentation):
pip install sanic_session
or if you prefer Pipenv
:
pipenv install sanic_session
Documentation
Documentation is available at sanic-session.readthedocs.io.
Also, make sure you read OWASP's Session Management Cheat Sheet for some really useful info on session management.
Example
A simple example uses the in-memory session interface.
from sanic import Sanic
from sanic.response import text
from sanic_session import Session, InMemorySessionInterface
app = Sanic()
session = Session(app, interface=InMemorySessionInterface())
@app.route("/")
async def index(request):
# interact with the session like a normal dict
if not request['session'].get('foo'):
request['session']['foo'] = 0
request['session']['foo'] += 1
return text(request['session']['foo'])
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)
Examples of using redis and memcache backed sessions can be found in the documentation, under Using the Interfaces.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sanic_session-0.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2d07d4b52dbde58f6bd36dc258badcae6730dad390525a74e0643dc1d9e0cc4 |
|
MD5 | 26d2de91201482aa86fc1ac936771e52 |
|
BLAKE2b-256 | 7a34ab632944eb25e950defe19c1e6d3340240a6ac9eef0847d56c91fba55aad |