tornado 服务器默认不做 favicon.ico,所以我总是得到这样的信息
[W 130626 10:38:16 web:1514] 404 GET /favicon.ico (192.168.1.57) 0.57ms
我以各种方式使用 web.staticfilehandler 包括源代码的示例,但无法使其工作,我的工作如下所示。
handlers = [
(r'/favicon.ico', tornado.web.StaticFileHandler,dict(url='/static/favicon.ico',permanent=False)),
(r'/static/(.*)', tornado.web.StaticFileHandler, {"path": "plserver"}),
]
我感觉很糟糕,我必须重定向它,并且无法确定它是否会在现实世界中的网页上工作。
我把它改成了这个,这次我得到了我想要的
handlers = [
(r'/(favicon.ico)', tornado.web.StaticFileHandler, {"path": ""}),
]
我在写这篇文章时得到了答案。
我将favicon.ico放在
.\static\
中,并将以下代码添加到html中。
<link rel="shortcut icon" href="{{ static_url('favicon.ico') }}">
它会像这样生成:
<link rel="shortcut icon" href="/static/favicon.ico?v=bb3f1">
仅此而已。
与
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static"),
}
handlers = [ (r'/', MainHandler) ] # or whatever you need ...
app = tornado.web.Application(handlers, **settings)
以及 favicon.ico 文件和静态子文件夹中的 robots.txt(可选),即使请求 URL 中没有 /static/,这两个文件也能正常工作。请参阅https://www.tornadoweb.org/en/stable/guide/running.html“静态文件和主动文件缓存”部分