Skip to main content

Test Package version Code coverage


Documentation: https://auredentan.github.io/starlette-session/


Starlette Session

Starlette session is a simple session middleware for starlette that enable server side session with starlette.

Requirements

Python 3.6+

Installation

pip install starlette-session

Example

Using redis as backend

from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route

from starlette_session import SessionMiddleware
from starlette_session.backends import BackendType

from redis import Redis

async def setup_session(request: Request) -> JSONResponse:
    request.session.update({"data": "session_data"})
    return JSONResponse({"session": request.session})


async def clear_session(request: Request):
    request.session.clear()
    return JSONResponse({"session": request.session})


def view_session(request: Request) -> JSONResponse:
    return JSONResponse({"session": request.session})


routes = [
    Route("/setup_session", endpoint=setup_session),
    Route("/clear_session", endpoint=clear_session),
    Route("/view_session", endpoint=view_session),
]

redis_client = Redis(host="localhost", port=6379)
app = Starlette(debug=True, routes=routes)
app.add_middleware(
    SessionMiddleware,
    secret_key="secret",
    cookie_name="cookie22",
    backend_type=BackendType.redis,
    backend_client=redis_client,
)

You can find more example here

Using a custom backend

You can provide a custom backend to be used. This backend has simply to implement the interface ISessionBackend

class ISessionBackend(ABC):
    @abstractmethod
    async def get(self, key: str) -> Optional[dict]:
        raise NotImplementedError()

    @abstractmethod
    async def set(self, key: str, value: dict, exp_in_mins: str) -> Optional[str]:
        raise NotImplementedError()

    @abstractmethod
    async def delete(key: str) -> Any:
        raise NotImplementedError()

Metadata

Release files for starlette-session 0.4.3

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

Source distribution (sdist)

Source distribution for starlette-session 0.4.3
File Size Uploaded
starlette_session-0.4.3.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for starlette-session 0.4.3
File Interpreter ABI Platform
starlette_session-0.4.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / starlette_session-0.4.3.tar.gz

Download URL starlette_session-0.4.3.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5b50a1c91a2ca1cf594574c0b386eea5dbed140dcb397ee73f0c43956bd2bfcc
BLAKE2b-256 checksum
How to use checksums
0a76e973d473cfabcce37d8bc3832de51f0b22dd29d49d41ce58ec6e7daadc9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.10.8 Linux/5.15.0-1022-azure

Release files / starlette_session-0.4.3-py3-none-any.whl

Download URL starlette_session-0.4.3-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bfc2559ec0fec566b44865f20d1c4262b1132de867a936abe8771cf139cbbe0b
BLAKE2b-256 checksum
How to use checksums
45fdeaff58a14a6b7a2dccd4ec6c82883bde5fcd9b256065c9e24423b8f33a12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.10.8 Linux/5.15.0-1022-azure

Release history Release notifications | RSS feed

This release

0.4.3 This release

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.10

2 release files

0.3.9

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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