尝试使烧杯会话在python瓶子框架中与gunicorn一起工作
我正在使用瓶子编写一个小型web程序,并将以下源文件命名为尝试使烧杯会话在python瓶子框架中与gunicorn一起工作,python,bottle,gunicorn,beaker,Python,Bottle,Gunicorn,Beaker,我正在使用瓶子编写一个小型web程序,并将以下源文件命名为index.py。我还在程序中使用烧杯会话库。当我使用python index.py运行代码时,一切正常。但是当我使用gunicorn-c gunicorn.conf index:app时,我会收到这样的错误消息,说烧杯键bicker.session不存在。如何更改代码以使gunicorn服务器再次工作 Traceback (most recent call last): File "/Library/Python/2.7/site-
index.py
。我还在程序中使用烧杯会话库。当我使用python index.py
运行代码时,一切正常。但是当我使用gunicorn-c gunicorn.conf index:app
时,我会收到这样的错误消息,说烧杯键bicker.session
不存在。如何更改代码以使gunicorn服务器再次工作
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/bottle-0.12.7-py2.7.egg/EGG-INFO/scripts/bottle.py", line 857, in _handle
self.trigger_hook('before_request')
File "/Library/Python/2.7/site-packages/bottle-0.12.7-py2.7.egg/EGG-INFO/scripts/bottle.py", line 640, in trigger_hook
return [hook(*args, **kwargs) for hook in self._hooks[__name][:]]
File "/Users/yizeng/Documents/python_projects/simple-courses/index.py", line 17, in setup_request
request.session = request.environ['beaker.session']
KeyError: 'beaker.session'
index.py的源代码
:
import os, sys, bottle
from bottle import debug, route, request, run, Bottle, static_file, hook
from apps import model, views
from beaker.middleware import SessionMiddleware
app = bottle.app()
session_options = {
'session.type': 'file',
'session.cookie_expires': 300,
'session.data_dir': './data',
'session.auto': True
}
@hook('before_request')
def setup_request():
request.session = request.environ['beaker.session']
@app.route('/assets/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='assets')
@app.route('/test')
def test():
s = request.environ.get('beaker.session')
s['test'] = s.get('test', 0) + 1
s.save()
return 'Test counter: %d' % s['test']
app_session = SessionMiddleware(app, session_options)
if __name__ == '__main__':
app.run(app=app_session)
导入操作系统、系统、瓶子
从瓶子导入调试、路由、请求、运行、瓶子、静态文件、钩子
从应用程序导入模型、视图
从bicker.middleware导入SessionMiddleware
app=瓶子。app()
会话_选项={
'session.type':'file',
“session.cookie_过期”:300,
“session.data_dir”:“./data”,
“session.auto”:True
}
@hook('before_request')
def设置_请求():
request.session=request.environ['biker.session']
@app.route(“/assets/”)
def服务器_静态(文件路径):
返回静态文件(filepath,root='assets')
@应用程序路径(“/test”)
def test():
s=request.environ.get('beaker.session')
s['test']=s.get('test',0)+1
s、 保存()
返回“测试计数器:%d”%s[“测试”]
应用程序会话=会话中间件(应用程序、会话选项)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(app=app\u会话)
我相信你根本不需要最后一块,如果你的名字…
。Gunicorn将索引模块的应用程序“varibale”作为WSIG运行,这意味着它应该启动瓶子应用程序的一个实例