Skip to main content

Sessions middleware plug-in for Bottle

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

BottleSessions-21.7.1.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

BottleSessions-21.7.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file BottleSessions-21.7.1.tar.gz.

File metadata

  • Download URL: BottleSessions-21.7.1.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for BottleSessions-21.7.1.tar.gz
Algorithm Hash digest
SHA256 d83c738a8ae664885a87c0aeb66e4ec8aeacf03c38f87630255497639dc10598
MD5 79fcb68b9ca933ad180aef65a8c9870c
BLAKE2b-256 694e5b875650dee3dcb66da5c5de65db47dff1eb750b1f33daf61f02b06cea5f

See more details on using hashes here.

File details

Details for the file BottleSessions-21.7.1-py3-none-any.whl.

File metadata

  • Download URL: BottleSessions-21.7.1-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for BottleSessions-21.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7c097dc5d0f2bfff2fd12ae5febde6b98547ed0ab6b269d1db14826dc933739
MD5 9a09464c728b024df7fffbed42d05e07
BLAKE2b-256 345acb1ce270ec4d698a2c115a7ad4b5f416a33399dd0fd4782e66da8cc12966

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page