Skip to main content

Provides server-backed sessions for Sanic using Redis, Memcache and more.

Project description

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

  • Memcache

  • In-Memory (suitable for testing and development environments)

Installation

Install with pip:

pip install sanic_session

Documentation

Documentation is available at PythonHosted.

Example

A simple example uses the in-memory session interface.

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


app = Sanic()
session_interface = InMemorySessionInterface()

@app.middleware('request')
async def add_session_to_request(request):
    # before each request initialize a session
    # using the client's request
    await session_interface.open(request)


@app.middleware('response')
async def save_session(request, response):
    # after each request save the session,
    # pass the response to set client cookies
    await session_interface.save(request, response)

@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


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-0.1.5.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sanic_session-0.1.5-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file sanic_session-0.1.5.tar.gz.

File metadata

  • Download URL: sanic_session-0.1.5.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sanic_session-0.1.5.tar.gz
Algorithm Hash digest
SHA256 ec8d3bb134cd5bdb3b58404e57f12a11ad7366517df65c675b60e64a3458574e
MD5 b81f8a916ef490c85dc1d1fcf991ed94
BLAKE2b-256 ea5913516bf4d72ff552899f320c3fe28f4829b3d3041bfc8ffe4c7a185296b0

See more details on using hashes here.

File details

Details for the file sanic_session-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for sanic_session-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4aabebf89dd4dcafe262137d4ae6e851a6d38a51f471caa7ab7216e222cd8b9b
MD5 520a96d436a4917d62bb27b035d79b3c
BLAKE2b-256 adcccd520b1a8d5e0e725e52ac48a7807c6d4eb81685875cfbc1696614436746

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page