python bottle框架使用beaker支持session
beaker下载地址:https://github.com/Lupino/bottle-session
import bottle
from bottle import request, run
from beaker.middleware import SessionMiddleware
app = bottle.default_app()
@app.get('/')
def hello():
s = request.environ.get('beaker.session')
print s
s['user'] = "wt"
s.save()
return "hello"
@app.get('/login')
def hello():
s = request.environ.get('beaker.session')
print s
return "hello"
session_opts = {
'session.type': 'file',
'session.cookie_expires': 100,
'session.data_dir': './session',
'session.auto': True
}
app = SessionMiddleware(app, session_opts)
run(app = app, host='219.246.178.240', port='9092', reloader = True)
访问日志 :
{'_accessed_time': 1434978090.149124, '_creation_time': 1434978071.703069}
192.168.1.240 - - [22/Jun/2015 21:01:30] "GET / HTTP/1.1" 200 5
{'_accessed_time': 1434978277.477368, 'user': 'wt', '_creation_time': 1434978071.703069}
192.168.1.240 - - [22/Jun/2015 21:04:37] "GET /login HTTP/1.1" 200 5
{'_accessed_time': 1434978434.063503, '_creation_time': 1434978434.063503}
192.168.1.240 - - [22/Jun/2015 21:07:14] "GET /login HTTP/1.1" 200 5