2017-05-02 3 views
0

私はPythonのトルネードで単純なサーバーを構築しています。
竜巻:AttributeError: 'StaticHandler'オブジェクトに属性 'absolute_path'がありません

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 
+0

は、あなたがそのエラーを取得する場合、アップグレードするには、ここ言う:

代わりのオーバーライドget

は、私はあなたがparse_url_pathを上書きしたいと思います)。 '$ sudo pip install --upgrade tornado' https://github.com/eudicots/Cactus/issues/77 – JacobIRR

+0

@JacobIRR実際に私はこの問題を読んで、竜巻をアップグレードしましたが、エラーはまだあります。彼らは同じ問題ではないかもしれません。私のエラーは 'tornado/web.py'から来ているのに対して、issue77のエラーは' server.py' – Yves

答えて

0

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.

問題ここでは、コードですここでは特に、getself.absolute_pathsee the code here)に設定されていますが、そのコードをy私たち自身の。このメッセージが表示された場合は、2.1から3.2のパッケージに(竜巻のパッケージを更新する必要があります

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