Skip to content

Instantly share code, notes, and snippets.

@oz123
Forked from thedemz/sessiontest.py
Created September 11, 2016 07:24
Show Gist options
  • Save oz123/62bfeb31244cd2ee6411c1ed2a0e00b7 to your computer and use it in GitHub Desktop.
Save oz123/62bfeb31244cd2ee6411c1ed2a0e00b7 to your computer and use it in GitHub Desktop.
Example of using beaker sessions with bottle
import bottle
from beaker.middleware import SessionMiddleware
session_opts = {
'session.type': 'memory',
'session.cookie_expires': 300,
'session.auto': True
}
app = SessionMiddleware(bottle.app(), session_opts)
@bottle.route('/')
def session_test():
s = bottle.request.environ.get('beaker.session')
s['test'] = 'this string came from the session'
s.save()
bottle.redirect('/output')
@bottle.route('/output')
def session_output():
s = bottle.request.environ.get('beaker.session')
return s['test']
bottle.run(
app=app,
host='localhost',
port=5000,
debug=True,
reloader=True
)
Using beaker in your bottle application is easy. First, set up your Bottle app:
import bottle
from bottle import request, route, hook
import beaker.middleware
session_opts = {
'session.type': 'file',
'session.data_dir': './session/',
'session.auto': True,
}
app = beaker.middleware.SessionMiddleware(bottle.app(), session_opts)
And later on:
bottle.run(app=app)
With this in place, every time you receive a request, your Beaker session will be available as request.environ['beaker_session']. I usually do something like this for convenience:
@hook('before_request')
def setup_request():
request.session = request.environ['beaker.session']
This arranges to run setup_request before handling any request; we're using the bottle.request variable (see the earlier import statement), which is a thread-local variable with information about the current request. From this point on, I can just refer to request.session whenever I need it, e.g.:
@route('/')
def index():
if 'something' in request.session:
return 'It worked!'
request.session['something'] = 1
# Beaker supports several different types of cache back-end: memory, filesystem, memcached and database.
# The supported database packages are: SQLite, SQLAlchemy and Google BigTable.
# session.type
#
# Accepts: string Default: dbm
#
# Options: "dbm", "file", "memcached", "database", "memory", "cookie"
#
# Storage Types: "dbm", "file", "memcached", "database", "memory"
# Sessions only Type: "cookie"
#
# The storage types uses the Container API that is also used by the cache system.
#
# When using dbm files, each user’s session is stored in its own dbm file,
# via the class :class”beaker.container.DBMNamespaceManager class.
#
# When using types: "database" or "memcached"
# additional Session "url" option are required.
# Type "database":
#
# session.url, string (formatted as required for an SQLAlchemy db uri) Default: None
#
# Unix/Mac - 4 initial slashes in total:
# SQLAlchemyURI = 'sqlite:////absolute/path/to/foo.db'
# Windows:
# SQLAlchemyURI = 'sqlite:///C:\\path\\to\\foo.db'
#
# Note: The appropriate database packages for the database must also be installed.
#
# session.table_name, string Default: beaker_cache
#
# Table name to use for beaker’s storage.
# The "cookie" type, requires the Sessions "secret" option to be set as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment