Created
March 27, 2014 16:35
-
-
Save oinume/9811874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# $ pip install beaker flask redis git+git://github.com/bbangert/beaker_extensions.git | |
from flask import Flask, session | |
from flask.sessions import SessionInterface | |
from beaker.middleware import SessionMiddleware | |
session_opts = { | |
'session.type': 'redis', | |
'session.url': '127.0.0.1:6379', | |
} | |
class BeakerSessionInterface(SessionInterface): | |
def open_session(self, app, request): | |
return request.environ['beaker.session'] | |
def save_session(self, app, session, response): | |
session.save() | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
if not session.has_key('value'): | |
session['value'] = 'Save in session' | |
return "Session value set." | |
else: | |
return session['value'] | |
if __name__ == '__main__': | |
app.wsgi_app = SessionMiddleware(app.wsgi_app, session_opts) | |
app.session_interface = BeakerSessionInterface() | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment