Server-backed sessions for Sanic with InMemory, Redis, Memcache, and MongoDB support
Project description
Sanic Session Management
⚠️ This package is a forked and updated version of
sanic-session0.8.0.
It includes important fixes such as resolving the "Cookies Initializing" warning.
Updated and maintained by Khaled Abdel Moezz from AIME GmbH.
sanic_sessions is a session management extension for Sanic that integrates server-backed sessions with a convenient API.
sanic_sessions provides a number of session interfaces for storing client session data. The available interfaces include:
- Redis (supports both
aioredisandasyncio_redis) - Memcache (via
aiomcache) - MongoDB (via
sanic_motorandpymongo) - In-Memory (suitable for testing and development environments)
Installation
Install with pip (additional options available for different drivers — check documentation):
pip install sanic_sessions
Usage Examples
In-Memory Session Example
A simple example using the in-memory session interface:
from sanic import Sanic
from sanic.response import text
from sanic_sessions import Session, InMemorySessionInterface
app = Sanic(name="ExampleApp")
session = Session(app, interface=InMemorySessionInterface())
@app.route("/")
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(str(request.ctx.session["foo"]))
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
Memcache Session Example
from sanic import Sanic
from sanic.response import text
from sanic_sessions import Session
from sanic_sessions.memcache import MemcacheSessionInterface
import aiomcache
app = Sanic(name="MemcacheExample")
# Set up a Memcache client
memcache_client = aiomcache.Client("127.0.0.1", 11211)
# Bind Memcache session interface
session = Session(app, interface=MemcacheSessionInterface(
cookie_name="session_id",
memcache_connection=memcache_client,
expiry=3600 # 1 hour
))
@app.route("/")
async def index(request):
if not request.ctx.session.get('visits'):
request.ctx.session['visits'] = 0
request.ctx.session['visits'] += 1
return text(f"Memcache session visit: {request.ctx.session['visits']}")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
💡 Note: To use Memcache sessions, make sure:
aiomcacheis installed:pip install aiomcache- Memcached server is running locally:
sudo apt install memcached sudo systemctl start memcached sudo systemctl status memcached
Redis Session Example
from sanic import Sanic
from sanic.response import text
from sanic_sessions import Session
from sanic_sessions.redis import RedisSessionInterface
from redis.asyncio import Redis
app = Sanic(name="RedisExample")
# Setup Redis connection
@app.before_server_start
async def setup_redis(app, _):
app.ctx.redis = Redis(host="127.0.0.1", port=6379)
await app.ctx.redis.ping()
print("✅ Redis connection established")
@app.after_server_stop
async def close_redis(app, _):
await app.ctx.redis.close()
# Attach Redis session interface
session = Session(app, interface=RedisSessionInterface(
redis_getter=lambda: app.ctx.redis,
cookie_name="redis_session",
expiry=3600
))
@app.route("/")
async def index(request):
if not request.ctx.session.get('hits'):
request.ctx.session['hits'] = 0
request.ctx.session['hits'] += 1
return text(f"Redis session hits: {request.ctx.session['hits']}")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
💡 Note: Make sure
redisis installed and running:
- Install Python driver:
pip install redis- Start Redis server:
sudo apt install redis sudo systemctl start redis sudo systemctl status redis
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sanic_sessions-0.9.0.tar.gz.
File metadata
- Download URL: sanic_sessions-0.9.0.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d9cfa4418ef9a20c21e33950c990f7a2b1cda0102bddde9ec0ec3f252511faa
|
|
| MD5 |
5c2bea9697a6da442aab0e020fce6feb
|
|
| BLAKE2b-256 |
954e6e7e8b0d4267f58ac56176ac735900eac228a150c933fa4083fb22a2d087
|
File details
Details for the file sanic_sessions-0.9.0-py3-none-any.whl.
File metadata
- Download URL: sanic_sessions-0.9.0-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e10d3756e790eb118dbb5c97286cc94d2ee0114659c8072a1c5261beb7b950b1
|
|
| MD5 |
b44a6f5f1399eb929233775d70d58f23
|
|
| BLAKE2b-256 |
3b6ecda3ecde19f53b2ceea2366991faf6330919c0b44b445b45d3df9a316179
|