Problems serving static files favicon.ico and robots.txt in CherryPy 3.1

牧云@^-^@ 提交于 2019-12-07 10:40:39

问题


When I try to browse to favicon.ico, for instance, I get this error:

ValueError: Static tool requires an absolute filename (got 'favicon.ico')

I can get to anything in my /images, /css and /js folders. Those are serving just fine. The site looks and acts great. It's just these darn two files.

Here is my root.conf file.

[/]
tools.staticdir.on = True
tools.staticdir.root = "/projects/mysite/root"
tools.staticdir.dir = ""

[/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"

Here is my cherrypy.conf file:

[global]
server.socket_port = 8888
server.thread_pool = 10
tools.sessions.on = True

Here's my "startweb.py" script:

import cherrypy
from root.roothandler import Root

cherrypy.config.update("cherrypy.conf")

cherrypy.tree.mount(Root(), "/", "root/root.conf")

if hasattr(cherrypy.engine, 'block'):
    # 3.1 syntax
    cherrypy.engine.start()
    cherrypy.engine.block()
else:
    # 3.0 syntax
    cherrypy.server.quickstart()
    cherrypy.engine.start()

回答1:


When you turn on a CherryPy Tool for a particular URL, it's turned on for all "child" URL's below it as well. So the [/images], [/css], and [/js] parts of your config seem redundant. So, too, is the [/robots.txt] section.

The [/favicon.ico] would be redundant, too, except favicon.ico is special because CherryPy sets one for you, typically (as an attribute of your root object; see _cptree.py). So overriding it is appropriate:

[/]
tools.staticdir.on = True
tools.staticdir.root = "/projects/mysite/trunk/root"
tools.staticdir.dir = ""
tools.staticfile.root = "/projects/mysite/trunk/root"

[/favicon.ico]
tools.staticfile.on = True
tools.staticfile.filename = "images/favicon.ico"



回答2:


I found one solution that works, but I don't like it much. It requires putting the full, absolute path in 3 places.

Here is the new root.conf

[/]
tools.staticdir.on = True
tools.staticdir.root = "/projects/mysite/trunk/root"
tools.staticdir.dir = ""

[/favicon.ico]
tools.staticfile.on = True
tools.staticfile.filename = "/projects/mysite/trunk/root/images/favicon.ico"

[/robots.txt]
tools.staticfile.on = True
tools.staticfile.filename = "/projects/mysite/trunk/root/robots.txt"

[/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"


来源:https://stackoverflow.com/questions/7308164/problems-serving-static-files-favicon-ico-and-robots-txt-in-cherrypy-3-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!