Search code examples
pythonhtmlcssservertornado

tornado: AttributeError: 'StaticHandler' object has no attribute 'absolute_path'


I'm building a simple server with python tornado. Here is the code:

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

However, when I visit my website, all of img and css files get an error:

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

Solution

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

    Subclasses should only override methods discussed in this section; overriding other methods is error-prone. Overriding StaticFileHandler.get is particularly problematic due to the tight coupling with compute_etag and other methods.

    To change the way static urls are generated (e.g. to match the behavior of another server or CDN), override make_static_url, parse_url_path, get_cache_time, and/or get_version.

    The problem here, in particular, is that get is supposed to set self.absolute_path (see the code here) but you've replaced that code with your own.

    Instead of overriding get, I think you want to override parse_url_path:

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