2011-03-25 163 views
5

我开始学习cherrypy,但我遇到了障碍。我无法获取静态文件来挽救我的生命。我得到了一个404. The path '/static' was not found.我google了但是还没有找到解决方案。我想要做的是提供文件http://localhost:8080/static无法从cherrypy提供静态文件

推荐?

import os 
import cherrypy 

class Root(object): 
    @cherrypy.expose 
    def index(self): 
     pass 

config = { 
    '/static':{ 
    'tools.staticdir.on': True, 
    'tools.staticdir.dir': os.path.join(os.path.dirname(__file__), 'static') 
    } 
} 

cherrypy.tree.mount(Root(), '/', config = config) 
cherrypy.engine.start() 

回答

6

一些想法:

  1. 在CherryPy的3.2+,尝试tools.staticdir.debug = True,与log.screen = True或其他一些更优选的记录设置相结合。这将比我在这个答案中猜到的任何东西都更有帮助。
  2. 尝试tools.staticdir.dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'));它需要是绝对的(或者,如果.dir不是绝对的,则需要tools.staticdir.root)。
  3. 在CherryPy 3.1及以上版本中,您通常需要在engine.start()之后调用engine.block()。
+0

我添加了调试和日志行,并且其检查的路径是绝对路径。这里是消息:'检查文件:e:\\ python \\ cherrypyapp \\ static \\'我再次检查了该路径及其正确但我仍然得到404 – Kylee 2011-03-25 12:43:18

+1

你实际浏览到'http:// localhost:8080/static',就好像你想要在浏览器中获取目录列表一样? staticdir工具不为它提供的文件提供索引页 - 你必须请求一个单独的文件,而不是一个目录。 – fumanchu 2011-03-26 01:44:40

+0

这是engine.block()做到了。谢谢! – Kylee 2011-03-26 12:21:14