问题描述
我想知道是否有更好的方法来使用 Tornado 处理我的 index.html 文件.
我使用 StaticFileHandler 处理所有请求,并使用特定的 MainHandler 处理我的主要请求.如果我只使用 StaticFileHandler 我得到一个 403: Forbidden 错误
GET http://localhost:9000/警告:root:403 GET/(127.0.0.1): 不是文件
这里是我现在的做法:
import os导入 tornado.ioloop导入 tornado.web从龙卷风进口网__author__ = 'gvincent'root = os.path.dirname(__file__)端口 = 9999类 MainHandler(tornado.web.RequestHandler):定义获取(自我):尝试:使用 open(os.path.join(root, 'index.html')) 作为 f:self.write(f.read())除了 IOError 作为 e:self.write("404: 未找到")application = tornado.web.Application([(r"/", MainHandler),(r"/(.*)", web.StaticFileHandler, dict(path=root)),])如果 __name__ == '__main__':application.listen(端口)tornado.ioloop.IOLoop.instance().start()
事实证明 Tornado 的 StaticFileHandler 已经包含默认文件名功能.
在 Tornado 版本 1.2.0 中添加了功能:https://github.com/tornadoweb/tornado/commit/638a151d96d681d3bdd42a5cf4bd681d35cf4b5c5c5bdc7b
要指定默认文件名,您需要将default_filename"参数设置为 WebStaticFileHandler 初始化的一部分.
更新您的示例:
导入操作系统导入 tornado.ioloop导入 tornado.webroot = os.path.dirname(__file__)端口 = 9999application = tornado.web.Application([(r"/(.*)", tornado.web.StaticFileHandler, {"path": root, "default_filename": "index.html"})])如果 __name__ == '__main__':application.listen(端口)tornado.ioloop.IOLoop.instance().start()
这将处理根请求:
/
->/index.html
子目录请求:
/tests/
->/tests/index.html
并且似乎可以正确处理目录的重定向,这很好:
/tests
->/tests/index.html
I want to know if there is a better way to handle my index.html file with Tornado.
I use StaticFileHandler for all the request,and use a specific MainHandler to handle my main request. If I only use StaticFileHandler I got a 403: Forbidden error
GET http://localhost:9000/
WARNING:root:403 GET / (127.0.0.1): is not a file
here how I doing now:
import os
import tornado.ioloop
import tornado.web
from tornado import web
__author__ = 'gvincent'
root = os.path.dirname(__file__)
port = 9999
class MainHandler(tornado.web.RequestHandler):
def get(self):
try:
with open(os.path.join(root, 'index.html')) as f:
self.write(f.read())
except IOError as e:
self.write("404: Not Found")
application = tornado.web.Application([
(r"/", MainHandler),
(r"/(.*)", web.StaticFileHandler, dict(path=root)),
])
if __name__ == '__main__':
application.listen(port)
tornado.ioloop.IOLoop.instance().start()
Turns out that Tornado's StaticFileHandler already includes default filename functionality.
Feature was added in Tornado release 1.2.0: https://github.com/tornadoweb/tornado/commit/638a151d96d681d3bdd6ba5ce5dcf2bd1447959c
To specify a default file name you need to set the "default_filename" parameter as part of the WebStaticFileHandler initialization.
Updating your example:
import os
import tornado.ioloop
import tornado.web
root = os.path.dirname(__file__)
port = 9999
application = tornado.web.Application([
(r"/(.*)", tornado.web.StaticFileHandler, {"path": root, "default_filename": "index.html"})
])
if __name__ == '__main__':
application.listen(port)
tornado.ioloop.IOLoop.instance().start()
This handles root requests:
/
->/index.html
sub-directory requests:
/tests/
->/tests/index.html
and appears to correctly handle redirects for directories, which is nice:
/tests
->/tests/index.html
这篇关于有没有更好的方法用 Tornado 处理 index.html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!