无法从cherrypy提供静态文件

时间:2011-03-25 01:38:16

标签: cherrypy

我开始学习樱桃,但我遇到了障碍。我无法获取静态文件来挽救我的生命。我得到了404. The path '/static' was not found.我用Google搜索但尚未找到解决方案。我想做的就是在http://localhost:8080/static

提供文件

Suggetions?

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

2 个答案:

答案 0 :(得分: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()。

答案 1 :(得分:0)

试试这个

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

    "/favicon.ico": {
        'tools.staticfile.on': True,
        'tools.staticfile.filename': "favicon.ico",
        'tools.staticdir.on': True,
        'tools.staticdir.dir': "images",
    },
    '/robots.txt': {
        'tools.staticfile.on': True,
        'tools.staticfile.filename': "robots.txt",
        'tools.staticdir.on': True,
        'tools.staticdir.dir': "", },

    '/images': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': "images",
    },

    '/css': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': "css",
    },

    '/js': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': "js",
    }
}

并开始于

if __name__ == "__main__":

    cherrypy.config.update(server_config)
    cherrypy.tree.mount(WebSpid(), config=web_config)

    if hasattr(cherrypy.engine, 'block'):
        # 3.1 syntax
        cherrypy.engine.start()
        cherrypy.engine.block() 
相关问题