Bottle的文档显示了如何使用烧杯进行会话管理,如下所示
import bottle
from beaker.middleware import SessionMiddleware
session_opts = {
'session.type': 'file',
'session.cookie_expires': 300,
'session.data_dir': './data',
'session.auto': True
}
app = SessionMiddleware(bottle.app(), session_opts)
@bottle.route('/test')
def test():
s = bottle.request.environ.get('beaker.session')
s['test'] = s.get('test',0) + 1
s.save()
return 'Test counter: %d' % s['test']
bottle.run(app=app)
我的问题是,我有多个瓶子应用程序,每个应用程序都服务于一个虚拟主机(由cherrypy提供支持)。所以我不能使用装饰“@ bottle.route”,相反,我需要使用装饰,如“app1.route('/ test')”,“app2.route('/ test')”。
但如果我用Beaker中间件扭曲应用程序,如下所示,
app1= Bottle()
app2= Bottle()
app1 = SessionMiddleware(app1, session_opts)
app2 = SessionMiddleware(app2, session_opts)
当python运行到以下时,
@app1.route('/test')
def test():
return 'OK'
它将报告错误,AttributeError:'SessionMiddleware'对象没有属性'route'
这是肯定的,因为现在app1实际上是一个'SessionMiddleware'而不是Bottle应用。
如何解决这个问题?
答案 0 :(得分:1)
在深入挖掘烧杯源代码后,我终于找到了方法。
以这种方式使用装饰器:
@app1.wrap_app.route('/test')
def test():
return 'OK'