2013-01-17 71 views
14


我想知道是否有更好的方式来处理我的index.html与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 

这里我现在该怎么做:

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

用'r“/ $”'和'r“/(.*)$”'替换'r“/”'和'r“/(.*)”'会更好。 – akaRem

回答

19

原来,龙卷风的StaticFileHandler已经包括默认的文件名功能。

功能在龙卷风版本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() 

这根处理请求:

  • / - >/index.html

子目录请求:

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

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

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

我可以在这些.html文件中使用模板标签吗? –

+0

这花了我大约5个小时才找到这个惊人的答案,感谢一千! – windsound

4

没有必要明确地添加StaticFileHandler;只需指定static_path,它将提供这些页面。

由于某些原因,即使您将文件名追加到URL,Tornado也不会为index.html文件提供服务,因此您是正确的您需要MainHandler。

在这种情况下,这种轻微的修改你的代码应该为你工作:

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): 
     self.render("index.html") 

application = tornado.web.Application([ 
    (r"/", MainHandler), 
    ], template_path=root, 
    static_path=root) 

if __name__ == '__main__': 
    application.listen(port) 
    tornado.ioloop.IOLoop.instance().start() 
+0

我无法在此解决方案的根文件夹中提供* .html文件。我也需要这些文件的处理程序。 –

+0

你使用的是哪种版本的Tornado?我测试了您的原始和我的答案,并且每个都可以提供任意* .html文件。我正在使用Tornado'version =“2.4.post1”','version_info =(2,4,0,1)'(在'__init __。py'中找到)。 –

+0

version =“2.4.1” version_info =(2,4,1,0)我使用相同的版本 –

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

而且Settings.py

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

STATIC_PATH = os.path.join(dirname, 'static') 
TEMPLATE_PATH = os.path.join(dirname, 'templates') 
+0

只需刷新浏览器就可以看到对'index.html'的更改并没有重新启动应用程序? – Xuan

+0

@轩我不明白你想让我做什么? –

+0

对不起,应该更清楚。在开发你的应用程序的过程中,你要对'index.html'进行更改,并且希望看到来自浏览器的更改。如果'index.html'是一个渲染模板而不是一个静态文件,你能看到刷新页面的变化吗? – Xuan

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) 

现在在您的应用程序中使用该类而不是香草StaticFileHandler ...作业已完成!

3

我一直在尝试这一点。不要使用渲染,它有解析模板的额外开销,并给出静态html中模板类型字符串的错误。我发现这是最简单的方法。龙卷风正在寻找正则表达式中的捕获括号,只是给它一个空的捕获组。

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"}) 
]) 

这有解析/ index.html的到,也避免不必要的做出决议像/views.html到static_dir/views.html

-1

这为我工作From the tornado docs的效果:

要在请求目录时自动提供index.html等文件,在您的应用程序设置中设置static_handler_args=dict(default_filename="index.html"),或者将default_filename作为StaticFileHandler的初始化参数添加。

相关问题