CherryPy WSGIServer提供静态文件的KeyError [英] KeyError with CherryPy WSGIServer serving static files
问题描述
我正在尝试使用CherryPy的WSGI服务器来提供静态文件,例如 每次我尝试击中服务器应该能够显示的任何内容时,此内容都会显示两次.当我将Flask应用程序连接到服务器时,Flask应用程序可以按预期工作,但是静态文件投放仍然出现相同的错误. 我需要怎么做才能使 我已经尝试了各种方法来使它正常工作,直到今天还遇到了您所看到的KeyError(以及其他问题). 通过修改此要点的静态文件(包括在下面). 希望包装Flask应用程序会非常相似. 对我来说,关键是在虚拟类上使用 出于好奇-我用要点中的代码自定义了django-cherrypy的runcpserver管理命令的版本,尽管事后看来,从头开始创建新命令可能会更容易. 祝您好运(并感谢Github上的 Alfredo Deza )!> I'm trying to use CherryPy's WSGI server to serve static files, like in Using Flask with CherryPy to serve static files. Option 2 of the accepted answer there looks exactly like what I'd like to do, but I'm getting a What I've tried: Then, when I try to access the server I'm getting a 500 response and the following error in the console: This is displayed twice for each time I try to hit anything that the server should be able to display. When I hooked up a Flask app to the server the Flask app worked as expected, but the static file serving still gave the same error. What do I need to do to get the I've tried various ways of getting this to work and up until today was also hitting the KeyError you have been seeing (among other issues). I finally managed to get CherryPy to serve static alongside a Django app by adapting the code from this gist (included below). Hopefully wrapping a Flask app will be fairly similar. The key for me was using the For the curious - I used the code in the gist to customise a version of django-cherrypy's runcpserver management command, although in hindsight it would probably have been easier to create a new command from scratch. Good luck (and thanks to Alfredo Deza)! 这篇关于CherryPy WSGIServer提供静态文件的KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! staticdir.handler
工作?
import os导入cherrypy从cherrypy导入wsgiserver从my_wsgi_app导入wsgiPATH = os.path.abspath(os.path.join(os.path.dirname(__ file__),'public'))类Root(object):经过def make_static_config(static_dir_name):"此处设置了所有自定义静态配置,因为大多数是通用的,因此它只生成一次是很有意义的."static_path = os.path.join('/',static_dir_name)路径= os.path.join(PATH,static_dir_name)配置= {static_path:{'tools.staticdir.on':是的,'tools.staticdir.dir':路径}}打印配置返回cherrypy.tree.mount(Root(),'/',config = configuration)#假设您的应用程序的媒体路径不同,例如"c","i"和"j"application = wsgiserver.WSGIPathInfoDispatcher({'/':wsgi.application,'/c':make_static_config('c'),'/j':make_static_config('j'),'/i':make_static_config('i')})服务器= wsgiserver.CherryPyWSGIServer(('0.0.0.0',8070),应用程序,server_name ='www.cherrypy.example')尝试:server.start()除了KeyboardInterrupt:打印正在终止服务器..."server.stop()
cherrypy.tree.mount
,而不是尝试直接使用staticdir.handler.KeyError
when I try to use the static directory handler.>>>> import cherrypy
>>>> from cherrypy import wsgiserver
>>>> import os
>>>> static_handler = cherrypy.tools.staticdir.handler(section='/', dir=os.path.abspath('server_files')
>>>> d = wsgiserver.WSGIPathInfoDispatcher({'/': static_handler})
>>>> server = wsgiserver.CherryPyWSGIServer(('localhost', 12345), d)
>>>> server.start()
KeyError('tools',)
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 1353, in communicate
req.respond()
File "/Library/Python/2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 868, in respond
self.server.gateway(self).respond()
File "/Library/Python/2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 2267, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "/Library/Python/2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 2477, in __call__
return app(environ, start_response)
File "/Library/Python/2.7/site-packages/cherrypy/_cptools.py", line 175, in handle_func
handled = self.callable(*args, **self._merged_args(kwargs))
File "/Library/Python/2.7/site-packages/cherrypy/_cptools.py", line 102, in _merged_args
tm = cherrypy.serving.request.toolmaps[self.namespace]
KeyError: 'tools'
staticdir.handler
to work?import os
import cherrypy
from cherrypy import wsgiserver
from my_wsgi_app import wsgi
PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), 'public'))
class Root(object):
pass
def make_static_config(static_dir_name):
"""
All custom static configurations are set here, since most are common, it
makes sense to generate them just once.
"""
static_path = os.path.join('/', static_dir_name)
path = os.path.join(PATH, static_dir_name)
configuration = {static_path: {
'tools.staticdir.on': True,
'tools.staticdir.dir': path}
}
print configuration
return cherrypy.tree.mount(Root(), '/', config=configuration)
# Assuming your app has media on diferent paths, like 'c', 'i' and 'j'
application = wsgiserver.WSGIPathInfoDispatcher({
'/': wsgi.application,
'/c': make_static_config('c'),
'/j': make_static_config('j'),
'/i': make_static_config('i')})
server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8070), application,
server_name='www.cherrypy.example')
try:
server.start()
except KeyboardInterrupt:
print "Terminating server..."
server.stop()
cherrypy.tree.mount
on a dummy class, rather than trying to use the staticdir.handler directly.