本文介绍了在CherryPy 3.1中提供静态文件favicon.ico和robots.txt的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,当我尝试浏览至favicon.ico时,出现此错误:

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

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

我可以进入/images,/css和/js文件夹中的任何内容.那些服务很好.该网站看起来和行为都很棒.只是这两个文件.

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.

这是我的root.conf文件.

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"

这是我的cherrypy.conf文件:

Here is my cherrypy.conf file:

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

这是我的"startweb.py"脚本:

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

推荐答案

当您为特定URL打开CherryPy工具时,其下面的所有子" URL也会打开.因此,配置的 [/images] [/css] [/js] 部分似乎是多余的.因此, [/robots.txt] 部分也是如此.

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.

[/favicon.ico] 也将是多余的,除了favicon.ico是特殊的,因为CherryPy通常为您设置一个(作为根对象的属性;请参见_cptree.py ).因此,覆盖它是适当的:

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"

这篇关于在CherryPy 3.1中提供静态文件favicon.ico和robots.txt的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 07:11