Skip to main content

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))

Metadata

Release files for bareASGI-session 5.0.0

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

Source distribution (sdist)

Source distribution for bareASGI-session 5.0.0
File Size Uploaded
bareasgi_session-5.0.0.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bareASGI-session 5.0.0
File Interpreter ABI Platform
bareasgi_session-5.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / bareasgi_session-5.0.0.tar.gz

Download URL bareasgi_session-5.0.0.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b0f341e7e41579d067035d20514bca46e9e814bab30c678fe3c02be9c11d2c09
BLAKE2b-256 checksum
How to use checksums
245b45abafcafd733c235012c773c5a67ec9345e444fe1a197c5527fe54bf498
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / bareasgi_session-5.0.0-py3-none-any.whl

Download URL bareasgi_session-5.0.0-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fde15867cc929f90c91511fb91e5aeba19c9d16eee0f473e2fcfc0bb6dde6073
BLAKE2b-256 checksum
How to use checksums
f3de77fa94a26ddf9b15f32535f0bc67cbf3f5abf6eb853dba1b3c44f41eb04b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13
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