我试图处理" /favicon.ico"但它没有按预期工作。它有线......
我的处理程序如下:
handlers = [
(r'^/$', RootHandler), # this works fine
(r'^/favicon\.ico$', IconHandler),
# other handlers
]
我的IconHandler
看起来像是:
class IconHandler(tornado.web.RequestHandler):
def get(self):
self.set_header("Content-Type", "image/vnd.microsoft.icon")
with open(icon_path, 'rb') as f:
self.write(f.read())
return self.flush()
但是http://127.0.0.1:8000/favicon.ico
给了我404错误:
Traceback (most recent call last):
File "/home/tyler/.pyenv/versions/3.4.2/lib/python3.4/site-packages/tornado/web.py", line 1334, in _execute
result = yield result
File "/home/tyler/.pyenv/versions/3.4.2/lib/python3.4/site-packages/tornado/gen.py", line 628, in run
value = future.result()
File "/home/tyler/.pyenv/versions/3.4.2/lib/python3.4/site-packages/tornado/concurrent.py", line 109, in result
raise_exc_info(self._exc_info)
File "<string>", line 3, in raise_exc_info
File "/home/tyler/.pyenv/versions/3.4.2/lib/python3.4/site-packages/tornado/gen.py", line 175, in wrapper
yielded = next(result)
File "/home/tyler/.pyenv/versions/3.4.2/lib/python3.4/site-packages/tornado/web.py", line 2110, in get
self.root, absolute_path)
File "/home/tyler/.pyenv/versions/3.4.2/lib/python3.4/site-packages/tornado/web.py", line 2286, in validate_absolute_path
raise HTTPError(404)
tornado.web.HTTPError: HTTP 404: Not Found
所以我更改了网址匹配模式并进行了一些测试:
handler pattern | url | status
-----------------------|---------------------------------------|--------
r'^/favicon\.ico$' | http://127.0.0.1:8000/favicon.ico | 404Error
r'/favicon\.ico' | http://127.0.0.1:8000/favicon.ico | 404Error
r'/favicon.ico' | http://127.0.0.1:8000/favicon.ico | 404Error
r'^/sub/favicon\.ico$' | http://127.0.0.1:8000/sub/favicon.ico | Works!
r'/sub/favicon\.ico' | http://127.0.0.1:8000/sub/favicon.ico | Works!
r'/sub/favicon.ico' | http://127.0.0.1:8000/sub/favicon.ico | Works!
请帮助。我无法理解:(
python:3.4.2
龙卷风:4.0.2
Ubuntu:14.10
答案 0 :(得分:2)
我相信您的应用设置中有static_path
字段。你的情况会发生什么:
您可以通过将static_path设置作为a发送来提供静态文件 关键字参数。
和
配置请注意,StaticFileHandler可以自动安装 static_path设置
static_path
设置后,会自动将Favicon模式插入到处理程序模式列表中。这些自动静态模式的优先级高于您自己的所有模式。
这是你的追溯证明:
File "/home/tyler/.pyenv/versions/3.4.2/lib/python3.4/site-packages/tornado/web.py", line 2286, in validate_absolute_path
raise HTTPError(404)
因此,我建议您从应用设置中删除static_path
,然后添加您自己的/static
模式
(r"/static/(.*)", web.StaticFileHandler, {"path": my_path}),