【问题标题】:tornado: AttributeError: 'StaticHandler' object has no attribute 'absolute_path'龙卷风:AttributeError:“StaticHandler”对象没有属性“absolute_path”
【发布时间】:2017-05-02 03:45:54
【问题描述】:

我正在使用 python tornado 构建一个简单的服务器。这是代码:

class IndexHandler(tornado.web.RequestHandler):
    def get(self, param):
        print("\n\nthis is a get request from indexhandler:")
        if param:
            #print("frontend/" + param)
            self.render("frontend/" + param)
            print("I'm html")
        else:
            print("index.html")
            self.render("index.html")

class StaticHandler(tornado.web.StaticFileHandler):
    def initialize(self, path, default_filename=None):
        self.root = os.path.abspath(path) + os.path.sep
        self.default_filename = default_filename

    def head(self, path):
        self.get(path, include_body=False)

    def get(self, param, include_body=True):
        abspath = "frontend/" + param
        print(abspath)
        myfile = open(abspath, "rb")
        try:
            self.write(myfile.read())
        finally:
            myfile.close()

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
                (r"/(.*jpg$)", StaticHandler, {"path": "/frontend"}),
                (r"/(.*css$)", StaticHandler, {"path": "/frontend"}),
                (r"/(.*html$)", IndexHandler)
                ]

        super(Application, self).__init__(handlers, **settings)


if __name__ == "__main__":
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

但是,当我访问我的网站时,所有 img 和 css 文件都会出现错误:

Traceback (most recent call last):
      File "/usr/local/lib/python3.5/site-packages/tornado/web.py", line 1513, in _execute
        self.finish()
      File "/usr/local/lib/python3.5/site-packages/tornado/web.py", line 973, in finish
        self.set_etag_header()
      File "/usr/local/lib/python3.5/site-packages/tornado/web.py", line 1416, in set_etag_header
        etag = self.compute_etag()
      File "/usr/local/lib/python3.5/site-packages/tornado/web.py", line 2436, in compute_etag
        version_hash = self._get_cached_version(self.absolute_path)
    AttributeError: 'StaticHandler' object has no attribute 'absolute_path'
[E 170502 11:44:48 web:2063] 500 GET /css/reset.css (108.61.177.156) 2.06ms

【问题讨论】:

  • 如果您收到该错误,请在此处表示升级:如果您收到此消息,您应该更新 tornado 包(从 2.1 到 3.2 包)。 $ sudo pip install --upgrade tornadogithub.com/eudicots/Cactus/issues/77
  • @JacobIRR 实际上我已经阅读了这个问题并升级了龙卷风,但错误仍然存​​在。他们可能不是同一个问题。因为我的错误来自tornado/web.py 而issue77 中的错误来自server.py

标签: python html css server tornado


【解决方案1】:

See the docs for StaticFileHandler, particularly "subclassing notes":

子类应该只覆盖本节讨论的方法;覆盖其他方法很容易出错。由于与 compute_etag 和其他方法的紧密耦合,覆盖 StaticFileHandler.get 尤其成问题。

要更改静态 url 的生成方式(例如,匹配另一个服务器或 CDN 的行为),请覆盖 make_static_url、parse_url_path、get_cache_time 和/或 get_version。

这里的问题尤其是,get 应该设置 self.absolute_path (see the code here),但您已经用自己的代码替换了该代码。

我认为您不想覆盖get,而是要覆盖parse_url_path

def parse_url_path(self, url_path):
    return 'frontend/' + url_path

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 2021-04-19
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多