Skip to main content

Bottle Sessions with Redis

Bottle_session is a session manager for the Bottle microframework that uses a cookie to maintain your web session and stores a hash associated with that cookie using the redis key-value store. It is designed as a simple Bottle plugin.

Installation

Install using either pip or easy_install:

$ pip install bottle-session

or you can download the latest version from bitbucket:

$ git clone https://devries@bitbucket.org/devries/bottle-session.git
$ cd bottle-session
$ python setup.py install

Requirements

In order to use bottle-session you must have both the redis and of course the bottle modules installed. I recommend also installing pycrypto, although it is not required. If pycrypto is installed, then the pycrypto random number generator is used to generate session cookies, otherwise python’s internal random number generator is used.

Using Bottle-session

The first requirement is that you import the bottle_session module:

import bottle_session
import bottle

Next, initialize the plugin:

app = bottle.app()
plugin = bottle_session.SessionPlugin(cookie_lifetime=600)
app.install(plugin)

The cookie_lifetime parameter is the lifetime of the cookie in seconds, if the lifetime is explicitly set to None it will last 1 week. The SessionPlugin class initializer takes several optional parameters:

  • host is the host for the redis instance. It defaults to localhost.

  • port is the port for the redis instance. It defaults to 6379.

  • db is the redis database number. It defaults to 0.

  • cookie_name is the name of the session cookie. It defaults to bottle.session.

  • cookie_secure is a boolean variable to set the Secure cookie flag. It defaults to False.

  • cookie_httponly is a boolean variable to set the HttpOnly cookie flag. It defaults to False.

  • keyword is the plugin keyword. It defaults to session.

  • password is the optional password for the redis instance. It defaults to none.

To use the plugin, just add the keyword (session by default) to the routed method:

@bottle.route('/')
def index(session):
    user_name = session.get('name')
    if user_name is not None:
        return "Hello, %s"%user_name
    else:
        return "I don't recognize you."

@bottle.route('/set/:user_name')
def set_name(session,user_name=None):
    if user_name is not None:
        session['name']=user_name
        return "I recognize you now."
    else:
        return "What was that?"

bottle.debug(True)
bottle.run(app=app,host='localhost',port=8888)

In this example you can set the name property of the session cookie to Chris by visiting the http://localhost:8888/set/Chris and then that value is retrieved when you visit http://localhost:8888/.

Using Bottle-session and Bottle-redis

If you are using redis for sessions you are likely using redis to store other data as well, and likely use the bottle-redis plugin. You can use both plugins together, and you can even get them to use the same connection pool. Initialize them by creating a connection pool which you attach to each plugin object before installing them into the bottle application as shown below:

#!/usr/bin/env python
import bottle_session
import bottle_redis
import bottle
import redis
from datetime import datetime

app = bottle.app()
session_plugin = bottle_session.SessionPlugin()
redis_plugin = bottle_redis.RedisPlugin()

connection_pool = redis.ConnectionPool(host='localhost', port=6379)

session_plugin.connection_pool = connection_pool
redis_plugin.redisdb = connection_pool
app.install(session_plugin)
app.install(redis_plugin)

@bottle.route('/')
def index(session,rdb):
    rdb.incr('visitors')
    visitor = rdb.get('visitors')
    last_visit = session['visit']
    session['visit'] = datetime.now().isoformat()

    return 'You are visitor %s, your last visit was on %s'%(visitor,last_visit)

bottle.debug(True)
bottle.run(app=app,host='localhost',port=8888)

Acknowledgments

Thanks to Marcel Hellkamp and the bottle community for the framework and to Sean M. Collins whose bottle-redis package in bottle-extras served as the inspiration for this bottle plugin. Thank you to James Burke for your contributions.

Metadata

Release files for bottle-session 1.0

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

Source distribution (sdist)

Source distribution for bottle-session 1.0
File Size Uploaded Provenance Actions
bottle-session-1.0.tar.gz 9.5 kB Without provenance Details for bottle-session-1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for bottle-session 1.0
File Interpreter ABI Platform Actions
bottle_session-1.0-py3.7.egg Legacy Egg format - - Details for bottle_session-1.0-py3.7.egg
bottle_session-1.0-py3-none-any.whl Python 3 none any Details for bottle_session-1.0-py3-none-any.whl

Total release size: 27.9 kB

Release files / bottle-session-1.0.tar.gz

Download URL bottle-session-1.0.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c682f72b51bbbdeba471d0f5810f81582349a07d4878861cddc0bd66ed4955dc
BLAKE2b-256 checksum
How to use checksums
ec58ae7c23f52bd105237501ee348a8d52d937c64a2131176c7d70d5a77d2f33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

Release files / bottle_session-1.0-py3.7.egg

Download URL bottle_session-1.0-py3.7.egg
Size 8.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
378d24e0ac2f2b18006266e86dc114e7af7eb8620a27c69e778d793efaf03b21
BLAKE2b-256 checksum
How to use checksums
dca21683f0e511e01ba3f89c8a9171ab60f847ae1882c81c1a8e428eecd135fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

Release files / bottle_session-1.0-py3-none-any.whl

Download URL bottle_session-1.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa74dfc4607e9808d1d77b57396cb722108fba1ca26051a1ce3b50f75eeb2700
BLAKE2b-256 checksum
How to use checksums
83078c915c11727c2a976cbfa837510c99bcd772f806f542ce7e4513c0adde37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

Security for bottle-session 1.0

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.

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