2015-03-30 88 views
1

我想使用CherryPy的WSGI服务器来提供静态文件,如Using Flask with CherryPy to serve static files。接受答案的选项2看起来完全像我想要做的,但是当我尝试使用静态目录处理程序时,我得到了一个KeyErrorKeyError与CherryPy WSGIServer服务静态文件

我已经试过:

>>>> 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() 

然后,当我尝试访问我得到一个500响应的服务器和下面的错误在控制台:

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' 

这是每次尝试打击服务器应能够显示的任何内容时会显示两次。当我将Flask应用程序连接到服务器时,Flask应用程序按预期工作,但静态文件服务仍然出现相同的错误。

我需要做些什么才能使staticdir.handler正常工作?

+0

我有同样的问题,你设法解决它? – 2015-04-02 11:17:41

+0

@ThomasTurner目前我有我的Flask应用程序服务静态文件,就像在[这个SO回答](http://stackoverflow.com/a/20648053/2216621)。答案是这不如从服务器直接提供服务(CherryPy在这种情况下),所以我仍然希望有人能够回答这个问题。 – 2015-04-02 17:20:24

回答

1

我已经尝试过各种方法来获得这个工作,直到今天还击中了你已经看到的KeyError(以及其他问题)。

我终于设法让CherryPy通过修改this gist(下面包含)中的代码来为Django应用程序提供静态服务。

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() 

希望包装一个Flask应用程序将非常相似。

对我来说关键是在虚拟类上使用cherrypy.tree.mount,而不是直接尝试使用staticdir.handler。

对于好奇 - 我在要点中使用代码来定制django-cherrypy的runcpserver管理命令的一个版本,尽管事后看来它可能会更容易从头开始创建新命令。

祝你好运(并感谢Alfredo Deza)!

+0

你应该在你的答案中包含相关的代码。链接到源代码很好,但你永远不知道它何时会消失。 – 2015-04-07 14:56:59

+0

代码现包含以供参考。 – robashdown 2015-04-07 15:32:44