Skip to main content

BottleSessions

BottleSessions is middleware providing web sessions for the Bottle micro web-framework.

The goal of BottleSessions is to provide easy to use and flexible to configure sessions. The defaults attempt to make sense for typical bottle web apps with little or no tuning.

BottleSessions is simple for the programmer to use in both middleware and route views. The Session is a superclass of a Python dict, accessible as an attribute added to the bottle request object as request.session. No special sauce is required to acquire or use the session.

Using the session

As an extension to dict the session is pythonic and is used like any other dict:

    user = request.session.get('user','Anonymous)
    ...
    request.session.update({
            'groups':['sysadmin','employee'], 
            'ip': request.ip
        })
    request.session['timestamp'] = time.now()

More details on using the BottleSessions.Session class are available.

Installation

Install from pypi:

pip install BottleSessions

BottleSessions Example

#app.py:

from BottleSessions import BottleSessions
from bottle import Bottle, request

app = Bottle()
btl = BottleSessions(app)

@app.route('/set/<key>/<val>')
def set_sess(key,val=None):

    request.session[key] = val
    return {key: val}

@app.route('/get/<key>')
def get_sess(key=None):

    return {key: request.session.get(key,'does not exist')}

@app.route('/')
def hello():
    return 'hello world'

if __name__ == '__main__':
    app.run()
#app.py:

from BottleSessions import BottleSessions
from bottle import Bottle, request

app = Bottle()
btl = BottleSessions(app)

@app.route('/set/<key>/<val>')
def set_sess(key,val=None):

    request.session[key] = val
    return {key: val}

@app.route('/get/<key>')
def get_sess(key=None):

    return {key: request.session.get(key,'does not exist')}

@app.route('/')
def hello():
    return 'hello world'

if __name__ == '__main__':
    app.run()

Another sample app is available here

BottleSession Defaults and Tuning

BottleSessions default behavior provides a session/cookie life of 300 seconds after last update using a cookie named bottlecookie marked Secure and http-only with path=/. The sessions use the same lifetime and are stored in a memory based cachelib SimpleCache.

These defaults are useful for a range of micro-framework web apps Bottle is typically used for. However, different applications have differing session needs. Hence both cookies and sessions can be easily customized to suite a variety of uses.

Backing Store

The backing store is provided by Pallets Project cachelib library and uses SimpleCache as the default.

cachelib FileSystemCache, RedisCache, and Memcached classes are also supported and can be configured with class specific options:

# config.py - FileSystemCache configuration
cache_config = {
    'cache_type': 'FileSystem',
    'cache_dir' : './sess_dir',
    'threshold': 2000,
    # Additional configuration parameters
    # per cachelib docs
    }
# app.py initialization
    ...
from config import cache_config
btl = BottleSessions(app, session_backing=cache_config,
session_cookie='appcookie')

Further information is available on configuring session backing store for differing needs and differing cache types.

Metadata

Release files for BottleSessions 21.9.21

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

Source distribution (sdist)

Source distribution for BottleSessions 21.9.21
File Size Uploaded
BottleSessions-21.9.21.tar.gz 15.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for BottleSessions 21.9.21
File Interpreter ABI Platform
BottleSessions-21.9.21-py3-none-any.whl Python 3 none any Details

Total release size: 26.1 kB

Release files / BottleSessions-21.9.21.tar.gz

Download URL BottleSessions-21.9.21.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9e25aee206eba856180e778ff4f153b9f75e6d9256739fe4b4543817b0927e25
BLAKE2b-256 checksum
How to use checksums
035e99a6cbca86acfed787dc896c74bbb4b2f6ddccbb04201c07ae06ecd65b06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

Release files / BottleSessions-21.9.21-py3-none-any.whl

Download URL BottleSessions-21.9.21-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8229ea58210df49b63fe69305df15a17fcc461902cc89fad78546d4eb9414ba9
BLAKE2b-256 checksum
How to use checksums
d28d41588ddb12a729a6aabdd6c188423cfdb6050b6908c76748293b42a063d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

21.9.21 This release

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