Skip to main content

Sanic Cookie and Session Management

Project description

Build Status Software License

Sanic Cookies

Code here is mostly borrowed from sanic_session.

I wanted to make some changes that would break a big part of sanic_session's API, so I decided to create this repo instead.

Sanic cookies supports both client side and server side cookies.

Some of the main deviations from sanic_session are:

  1. Interfaces are only responsible for reading/writing the session_dict. Session management logic is handled by the session object

  2. No race conditions:

    By using:

     async with request['session']:
         request['session']['foo'] = 'bar'
    

    instead of:

     request['session']['foo'] = 'bar'
    

    It is still however possible to use the session_dict without a context manager, but it will raise some warnings, unless it's explicitly turned off (warn_lock=False)

  3. A more simple implementation of SessionDict that helps me sleep in peace at night. (Probably less performant)

  4. In memory interface schedules cleanup to avoid running out of memory

  5. Client side cookies

Quick Start

from sanic_cookies import Session, InMemory

interface = InMemory()
app = Sanic()
Session(app, master_interface=interface)

# You can skip this part if you don't want scheduled interface cleanup
@app.listener('before_server_start')
def init_inmemory(app, loop):
    interface.init()
@app.listener('after_server_stop')
def kill_inmemory(app, loop):
    interface.kill()

@app.route('/')
async def handler(request):
    async with request['session'] as sess:
        sess['foo'] = 'bar'

If enough people are interested, I can write some docs. Meanwhile, if you excuse me...

Gotta go fast!!

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_cookies-0.2.5.tar.gz (11.5 kB view details)

Uploaded Source

File details

Details for the file sanic_cookies-0.2.5.tar.gz.

File metadata

  • Download URL: sanic_cookies-0.2.5.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.6.7

File hashes

Hashes for sanic_cookies-0.2.5.tar.gz
Algorithm Hash digest
SHA256 692249e11ceb037756c482e6a9a25fbf39ac19d1a8e8b6cdde346f26b479dd97
MD5 9e39d42470330325a36082a6ad184794
BLAKE2b-256 963c448af07877979592e2a0a2d7d631fcd59d0cb6a7b53a975b14f00ea443f9

See more details on using hashes here.

Provenance

Supported by

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