2014-03-03 63 views
3

为什么这一工作并不:不能得到龙卷风staticfilehandler工作

application = tornado.web.Application([(r"/upload.html",tornado.web.StaticFileHandler,\ 
             {"path":r"../web/upload.html"}),])  
if __name__ == "__main__": 
    print "listening" 
    http_server = tornado.httpserver.HTTPServer(application) 
    http_server.listen(8888) 
    tornado.ioloop.IOLoop.instance().start() 

击中

http://localhost:8888/upload.html throws: 

TypeError: get() takes at least 2 arguments (1 given) 
ERROR:tornado.access:500 GET /upload.html (::1) 6.47ms 

我曾尝试在互联网上进行搜索,但好像我的使用是完全正确的。 所以我找不到为什么它不工作。互联网上的大多数例子都是关于为一个完整的目录提供一个静态处理程序。那么情况就是这样,它不适用于单个文件?

回答

4

你有两个选择来解决这个错误。

  1. 添加../web/目录中的所有文件。龙卷风不处理单个文件。

    application = tornado.web.Application([(r"/(.*)", \ 
                 tornado.web.StaticFileHandler, \ 
                 {"path":r"../web/"}),]) 
    
  2. 您可以渲染HTML作为输入文件。您需要为每个HTML文件创建一个处理程序。

    import tornado.web 
    import tornado.httpserver 
    
    
    class Application(tornado.web.Application): 
        def __init__(self): 
         handlers = [ 
          (r"/upload.html", MainHandler) 
         ] 
         settings = { 
          "template_path": "../web/", 
         } 
         tornado.web.Application.__init__(self, handlers, **settings) 
    
    
    class MainHandler(tornado.web.RequestHandler): 
        def get(self): 
         self.render("upload.html") 
    
    
    def main(): 
        applicaton = Application() 
        http_server = tornado.httpserver.HTTPServer(applicaton) 
        http_server.listen(8888) 
    
        tornado.ioloop.IOLoop.instance().start() 
    
    if __name__ == "__main__": 
        main() 
    
3

StaticFileHandler通常用于提供目录,因此它期望接收路径参数。从the docs

注意,在正则表达式的捕获组是必需的解析值 用于路径参数给get()方法(除上述 构造器参数不同);有关详细信息,请参阅URLSpec。

例如,

urls = [(r"/(.*)", tornado.web.StaticFileHandler, {"path": "../web"})] 
application = tornado.web.Application(urls) 

将服务于../web中的每个文件,包括upload.html。

+3

如果你只是想提供一个鱼贯而出的目录,你可以通过将在正则表达式的文件名和目录的路径参数做到这一点:'[(R“/(上传\ .html)“,StaticFileHandler,{”path“:”../ web“})]' –