Quart-Session
Quart-Session is an extension for Quart that adds support for server-side sessions to your application.
Based on flask-session.
Quick start
Quart-Session can be installed via pipenv or pip,
$ pipenv install quart-session
$ pip install quart-session
and requires Python 3.7.0 or higher. A fairly minimal Quart-Session example is,
from quart import Quart, session
from quart_session import Session
app = Quart(__name__)
app.config['SESSION_TYPE'] = 'redis'
Session(app)
@app.route('/')
async def hello():
session["foo"] = "bar"
return 'hello'
app.run()
Features
Redis support
via aioredis.
app = Quart(__name__)
app.config['SESSION_TYPE'] = 'redis'
Session(app)
If you already have a aioredis.Client instance and you'd like to share
it with the session interface,
app = Quart(__name__)
app.config['SESSION_TYPE'] = 'redis'
@app.before_serving
async def setup():
cache = await aioredis.create_redis_pool(...)
app.config['SESSION_REDIS'] = cache
Session(app)
By default, Quart-session creates a single connection to Redis, while the example above sets up a connection pool.
Trio support
Quart-Session comes with an (experimental) Redis client for use with the Trio eventloop.
from quart_trio import QuartTrio
from quart_session.redis_trio.client import RedisTrio
app = QuartTrio(__name__)
app.config['SESSION_TYPE'] = 'redis'
Session(app)
Memcached support
via aiomcache.
app = Quart(__name__)
app.config['SESSION_TYPE'] = 'memcached'
Session(app)
JSON serializer
flask-session uses pickle
for session data, Quart-Session opts for a JSON serializer capable of
(de)serializing the usual JSON types, as well as: Tuple, Bytes,
Markup, UUID, and DateTime.
JSON as session data allows for greater interoperability with other programs/languages that might want to read session data straight from a back-end.
If for some unholy reason you prefer pickle or your own serializer,
app = Quart(__name__)
app.config['SESSION_TYPE'] = 'redis'
Session(app)
try:
import cPickle as pickle
except ImportError:
import pickle
app.session_interface.serialize = pickle
Session control
By default, flask-session sets a session for each incoming request, including static files. From experience, this approach can put unneeded load on underlying session infrastructure, especially in high-traffic environments.
Quart-Session offers control over the session creation. For example, often you'll only need to create a session when a user successfully logs in.
To enable this behaviour, set SESSION_EXPLICIT to True.
app = Quart(__name__)
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_EXPLICIT'] = True
Session(app)
@app.route('/')
async def root():
if session.get('authenticated'):
return "Welcome back!"
return "Welcome anonymous!"
@app.route('/login')
async def login():
session["authenticated"] = True
session.dirty() # mark session for saving
return 'Logged in!'
app.run()
To re-gain the old behaviour of always emitting a Set-Cookie header on static file serves,
set SESSION_STATIC_FILE to True.
Session pinning
Associates an user's session to his/her IP address. This mitigates cookie stealing via XSS etc, and is handy for paranoid web applications.
app = Quart(__name__)
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_HIJACK_PROTECTION'] = True
Session(app)
Session reuse from a different IP will now result in the creation of a new session, and the deletion of the old.
Important: If your application is behind a reverse proxy, it most
likely provides the X-Forwarded-For header which you must make use of
by explicitly setting SESSION_HIJACK_REVERSE_PROXY to True.
Future development
The following session interfaces would be nice to have:
MongoDBSessionInterfaceFileSystemSessionInterfaceGoogleCloudDatastoreSessionInterface
Other to-do's:
- Unit testing
- Documentation (Sphinx)
Migrating from Flask
This library works very similarly to flask-session.
The quart_session.sessions APIs are not 100% the same, but unless you
are embedded in Flask-Session's internals, a migration should be fairly
straightforward. The distinct changes are specified below:
- Quart-Session does not
Set-Cookieon (static) files by default. - Quart-Session might not have all the back-end interfaces implemented (yet), such as "filesystem".
- Quart-Session uses a different serializer:
quart.json.tag.TaggedJSONSerializerinstead ofpickle. - Quart-Session disallows the client to supply their own made up
sidcookie value.
Help
Find the Quart folk on gitter or open an issue.
License
BSD
Metadata
Release files for Quart-Session 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| Quart-Session-0.0.1.tar.gz | 11.7 kB | Without provenance | Details for Quart-Session-0.0.1.tar.gz |
Release files / Quart-Session-0.0.1.tar.gz
| Download URL | Quart-Session-0.0.1.tar.gz |
|---|---|
| Size | 11.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c815bfd941f76d74f2602158886db6ba5ddd2439ebf7b63a995e8d9e24ffe360
|
|
BLAKE2b-256 checksum How to use checksums |
9574d49bb2114ed7db32c0a1d7939a268e36a109b82f0b156c33c8c421fa4053
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5
|
Security for Quart-Session 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.