⚠️ Warning: This poject in Pull-Request-Only mode. I don't use Sanic anymore for any of my projects and will not make any changes by my own in foreseeable future. I recommend to use AIOHTTP since it more mature and have bigger adoption (I'm not judging by github stars only). Though I will accept pull requests from others if you want to see updates in the code.
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:
pip (there is other options for different drivers, check documentation):
pip install sanic_session
or if you prefer
pipenv install sanic_session
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.
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()) async def index(request): # interact with the session like a normal dict if not request.ctx.session.get('foo'): request.ctx.session['foo'] = 0 request.ctx.session['foo'] += 1 return text(request.ctx.session['foo']) if __name__ == "__main__": app.run(host="0.0.0.0", port=8000)
Examples of using redis and memcache backed sessions can be found in the documentation, under Using the Interfaces.
— ⭐️ —