是否有更好的方法来处理与龙卷风的index.html?

Gui*_*ent 17 python tornado


我想知道是否有更好的方法来处理我的Tornado index.html文件.

我对所有请求使用StaticFileHandler,并使用特定的MainHandler来处理我的主要请求.如果我只使用StaticFileHandler,我得到了403:Forbidden错误

GET http://localhost:9000/
WARNING:root:403 GET / (127.0.0.1):  is not a file
Run Code Online (Sandbox Code Playgroud)

这是我现在的表现:

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()
Run Code Online (Sandbox Code Playgroud)

小智 23

事实证明,Tornado的StaticFileHandler已经包含默认文件名功能.

功能在Tornado版本1.2.0中添加:https: //github.com/tornadoweb/tornado/commit/638a151d96d681d3bdd6ba5ce5dcf2bd1447959c

要指定默认文件名,需要将"default_filename"参数设置为WebStaticFileHandler初始化的一部分.

更新你的例子:

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()
Run Code Online (Sandbox Code Playgroud)

这处理根请求:

  • / - > /index.html

子目录请求:

  • /tests/ - > /tests/index.html

并且似乎正确处理目录的重定向,这很好:

  • /tests - > /tests/index.html


Gui*_*ent 12

感谢前面的回答,这是我更喜欢的解决方案:

import Settings
import tornado.web
import tornado.httpserver


class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/", MainHandler)
        ]
        settings = {
            "template_path": Settings.TEMPLATE_PATH,
            "static_path": Settings.STATIC_PATH,
        }
        tornado.web.Application.__init__(self, handlers, **settings)


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html")


def main():
    applicaton = Application()
    http_server = tornado.httpserver.HTTPServer(applicaton)
    http_server.listen(9999)

    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

和Settings.py

import os
dirname = os.path.dirname(__file__)

STATIC_PATH = os.path.join(dirname, 'static')
TEMPLATE_PATH = os.path.join(dirname, 'templates')
Run Code Online (Sandbox Code Playgroud)


小智 5

请改用此代码

class IndexDotHTMLAwareStaticFileHandler(tornado.web.StaticFileHandler):
    def parse_url_path(self, url_path):
        if not url_path or url_path.endswith('/'):
            url_path += 'index.html'

        return super(IndexDotHTMLAwareStaticFileHandler, self).parse_url_path(url_path)
Run Code Online (Sandbox Code Playgroud)

现在在你的应用程序中使用该类而不是vanilla StaticFileHandler ...工作完成了!