Adds server-side session support to your Starlette application
Project description
starlette-sessions
Adds server-side session support to your Starlette application
License
Contributors
- Jesse McLaughlin nzjess@gmail.com
Installing
# with redis support, for example
# from source
poetry install --extras redis
Quick Start
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route
import uvicorn
from starlette_sessions.middleware import SessionMiddleware
from starlette_sessions.cookie.backend import CookieSessionBackend
from starlette_sessions.signedcookie.backend import SignedCookieSessionBackend
from starlette_sessions.memory.backend import MemorySessionBackend
from starlette_sessions.redis.backend import RedisSessionBackend
async def homepage(request: Request):
return JSONResponse({"hello": "world"})
async def session_set(request: Request):
counter = request.session.get("counter")
response = JSONResponse({"counter": counter})
request.session["counter"] = (counter or 0) + 1
return response
async def session_read(request: Request):
return JSONResponse(
{k: v for k, v in request.session.items()}
)
async def session_clear(request: Request):
counter = request.session.get("counter")
response = JSONResponse({"counter": counter})
request.session.clear()
return response
async def session_unsafe(request: Request):
try:
counter = request.session["counter"]
except KeyError:
counter = None
return JSONResponse({"counter": counter})
#
# try out these backends:
#
session_backend = CookieSessionBackend()
# session_backend = SignedCookieSessionBackend(secret_key="s3cr3t", max_age=30)
# session_backend = MemorySessionBackend()
# session_backend = RedisSessionBackend(ttl=30)
app = Starlette(
debug=True,
routes=[
Route("/", homepage),
Route("/set", session_set),
Route("/read", session_read),
Route("/clear", session_clear),
Route("/unsafe", session_unsafe),
],
middleware=[
Middleware(SessionMiddleware, backend=session_backend),
]
)
uvicorn.run(app, host="0.0.0.0", port=8080)
Development
# setup
poetry install --with dev --all-extras
# linting
poetry run ruff src tests
# formatting
poetry run black src tests
poetry run isort src tests
# type checking
poetry run mypy src tests
# testing
poetry run pytest -s tests/unit
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 starlette_sessions-0.3.0.tar.gz
.
File metadata
- Download URL: starlette_sessions-0.3.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb3700c7903b88154c867526f39da1408439c671311c9c371fed539f3e14986b |
|
MD5 | 117d265bed3f3e2526049541b6327016 |
|
BLAKE2b-256 | 36c5dd8c349a0ea99030918c5a29856905178691e3b7fdb55e0259161ed54b66 |
Provenance
The following attestation bundles were made for starlette_sessions-0.3.0.tar.gz
:
Publisher:
release-pypi.yml
on nzjess/starlette-sessions
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
starlette_sessions-0.3.0.tar.gz
- Subject digest:
eb3700c7903b88154c867526f39da1408439c671311c9c371fed539f3e14986b
- Sigstore transparency entry: 145888660
- Sigstore integration time:
- Predicate type:
File details
Details for the file starlette_sessions-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: starlette_sessions-0.3.0-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03750f1c5d31f38996c4ff3678ff4866f948258197d258e34f6c327d9527144f |
|
MD5 | 367794e6b9456c89010fd7f901e7b799 |
|
BLAKE2b-256 | 5c0d0e8861d52715e24d6af71eceea374ed86fc2c44c137c910001252db8d45d |
Provenance
The following attestation bundles were made for starlette_sessions-0.3.0-py3-none-any.whl
:
Publisher:
release-pypi.yml
on nzjess/starlette-sessions
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
starlette_sessions-0.3.0-py3-none-any.whl
- Subject digest:
03750f1c5d31f38996c4ff3678ff4866f948258197d258e34f6c327d9527144f
- Sigstore transparency entry: 145888661
- Sigstore integration time:
- Predicate type: