Skip to main content

Session support for bareASGI

Project description

bareASGI-session

Session support for bareASGI (read the docs).

Overview

When a client (e.g. a browser tab) makes HTTP requests to a server, the server does not know if the requests came from the same client. This makes it difficult to maintain state information (e.g. the users identity).

A solution which is transparent to the client involves the server sending a cookie to the client. Once the cookie is sent the client will automatically add the cookie to any subsequent request it makes to the server (assuming cookies are enabled). By checking the cookie the server can know which client has sent the request.

Usage

You can add session middleware with the add_session_middleware helper function.

from bareasgi import Application
from bareasgi_session import (
  add_session_middleware,
  MemorySessionStorage
)

app = Application()

add_session_middleware(app, MemorySessionStorage())

The session can be retrieved with the session_data helper function. This returns an (initially empty) dictionary.

from datetime import datetime
from bareutils import text_writer
from bareasgi_session import session_data

async def session_handler(request: HttpRequest) -> HttpResponse:
    session = session_data(request)
    now = session.get('now')
    message = f'The time was {now}' if now else 'First time'
    session['now'] = datetime.now()
    headers: List[Header] = [
        (b'content-type', b'text/plain'),
        (b'content-length', str(len(message)).encode('ascii'))
    ]
    return HttpResponse(200, headers, text_writer(message))

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

bareASGI-session-4.0.0a3.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

bareASGI_session-4.0.0a3-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file bareASGI-session-4.0.0a3.tar.gz.

File metadata

  • Download URL: bareASGI-session-4.0.0a3.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.8.10 Linux/5.4.0-84-generic

File hashes

Hashes for bareASGI-session-4.0.0a3.tar.gz
Algorithm Hash digest
SHA256 9e826b7346fb765f8383efd0b63e6b8b2dabf40b6f12b5a21b298d2e17e3ae50
MD5 683747d5a96fb290487d59bee61939f2
BLAKE2b-256 a0f0549def270f9d60e33da692e8b9c44007978a16c27be32c194a830c6df6f6

See more details on using hashes here.

File details

Details for the file bareASGI_session-4.0.0a3-py3-none-any.whl.

File metadata

  • Download URL: bareASGI_session-4.0.0a3-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.8.10 Linux/5.4.0-84-generic

File hashes

Hashes for bareASGI_session-4.0.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 e72d9dd82ae87f122dd74c074efe87daef7f92a8f1ccec2573618312b425faee
MD5 f0f969e42d014142df741cd47b6ab71e
BLAKE2b-256 72b1b846a8d6ee7789bc7ac29b5cb84d1b076de266ea0f425420aafe1bc3d64b

See more details on using hashes here.

Supported by

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