Provides server-backed sessions for Sanic using Redis, Memcache, Mongo, in-memory.
Project description
### 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)
```
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)
```
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
sanic_session_2-0.2.6.tar.gz
(7.1 kB
view details)
File details
Details for the file sanic_session_2-0.2.6.tar.gz
.
File metadata
- Download URL: sanic_session_2-0.2.6.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0251016dafd658f3bd9d40c47df5debc2045e7392372b4971636ec85da21fe85 |
|
MD5 | ca4f07d5ff4a9f6d3aae3a10719aea79 |
|
BLAKE2b-256 | 598da05a22bfdba7175ba0942dccdabacc61bea71c4ee6ea8d85c89fbfd1419a |