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
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
Built Distribution
File details
Details for the file bareASGI-session-4.0.1.tar.gz
.
File metadata
- Download URL: bareASGI-session-4.0.1.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.9.6 Darwin/19.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d556b6fde8ca1d2401ae2188b52158452e97427745355fc6484a1fdb7b04cfb2 |
|
MD5 | 8fc36d195d9b2a304592df2d08ffe9af |
|
BLAKE2b-256 | 6f5ff7aa073c7d7816578fb10d4e92b7f04d11a89b5003701c2e90680f3de960 |
File details
Details for the file bareASGI_session-4.0.1-py3-none-any.whl
.
File metadata
- Download URL: bareASGI_session-4.0.1-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.9.6 Darwin/19.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fd888f9891c5eb3cce94424c97ff27d2b845996858ec2dbeb9b26722aa3ac2b |
|
MD5 | b083640283a1f18fd5a147d60885c588 |
|
BLAKE2b-256 | df4be93a7cf935a7348a772bf77ecaee36572ec714270773bd67b9f289e02322 |