Skip to main content
### sanic_session

sanic_session is an extension for sanic that integrates server-backed sessions with a Flask-like 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 (using aioredis, asyncio_redis)
* Memcache (using aiomemcache)
* MongoDB (using sanic_motor)
* In-Memory (suitable for testing and development environments)

Forked from: https://github.com/subyraman/sanic_session
Added more easy installation, fixed code a little bit.

## Installation

Install with `pip`:

`pip install sanic_session`

## Examples

A simple example uses the in-memory session interface.


```python
from sanic import Sanic
from sanic.response import text
import sanic_session


app = Sanic()
sanic_session.install_middleware(app, '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)
```

Using sanic_session with Redis via asyncio_redis.

Install sanic_session to use with asyncio_redis driver: `pip install sanic_session[asyncio_redis]`


```python
import asyncio_redis

from sanic import Sanic
from sanic.response import text
import sanic_session


app = Sanic()


# general (for all app) redis pool
# (should be filled asynchronously after server start)
asyncio_redis_pool = None

@app.listener ('before_server_start')
async def general_before_server_start (app, loop):
global asyncio_redis_pool
asyncio_redis_pool = await asyncio_redis.Pool.create(host='127.0.0.1', port=6379, poolsize=2)
sanic_session.install_middleware (app, 'AsyncioRedisSessionInterface', asyncio_redis_pool)

@app.listener ('after_server_stop')
async def general_after_server_stop (app, loop):
asyncio_redis_pool.close ()


@app.route("/")
async def test(request):
# interact with the session like a normal dict
if not request['session'].get('foo'):
request['session']['foo'] = 0

request['session']['foo'] += 1

response = text(request['session']['foo'])

return response

if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)
```


Using sanic_session with Memcache via aiomemcache.

Install sanic_session to use with aiomemcache driver: `pip install sanic_session[aiomcache]`


```python
import aiomcache
import uvloop

from sanic import Sanic
from sanic.response import text
import sanic_session

app = Sanic()


# create a uvloop to pass into the memcache client and sanic
loop = uvloop.new_event_loop()

# create a memcache client
client = aiomcache.Client("127.0.0.1", 11211, loop=loop)

# install sanic_session middleware with memcache client
sanic_session.install_middleware (app, 'MemcacheSessionInterface', client)


@app.route("/")
async def test(request):
# interact with the session like a normal dict
if not request['session'].get('foo'):
request['session']['foo'] = 0

request['session']['foo'] += 1

response = text(request['session']['foo'])

return response

if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True, loop=loop)
```

Release files for sanic_session_2 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic_session_2 0.2.6
File Size Uploaded
sanic_session_2-0.2.6.tar.gz 7.1 kB Details

Release files / sanic_session_2-0.2.6.tar.gz

Download URL sanic_session_2-0.2.6.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
0251016dafd658f3bd9d40c47df5debc2045e7392372b4971636ec85da21fe85
BLAKE2b-256 checksum
How to use checksums
598da05a22bfdba7175ba0942dccdabacc61bea71c4ee6ea8d85c89fbfd1419a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release history Release notifications | RSS feed

This release

0.2.6 This release

1 release file

0.2.5

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page