Search code examples
pythonpython-2.7tornadostatic-files

HTTP 404 while using tornado's StaticFileHandler class


I'm getting the following traceback while using tornado's StaticFileHandler class. Traceback:

    Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1334, in _execute
    result = yield result
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 628, in run
    value = future.result()
  File "/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py", line 109, in result
    raise_exc_info(self._exc_info)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 175, in wrapper
    yielded = next(result)
  File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 2110, in get
    self.root, absolute_path)
  File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 2286, in validate_absolute_path
    raise HTTPError(404)
HTTPError: HTTP 404: Not Found

This is the structure of my directory :

Server/

  • httpserver.py
  • static/static/foo/bar/files.extension

The content of httpserver.py :

settings = {
'debug': True,
'autoreload': True,
}

application = tornado.web.Application([\
            (r"/(.*)",tornado.web.StaticFileHandler,\
            {"path":"static/static/foo/bar/"}),],\
            **settings)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

I've gone through the similar questions, they don't seem to give me a proper solution.


Solution

  • I got this problem fixed by myself. I added a keyword argument static_path, a path from root to the directory from where the .py file is. The corrected, working code :

    settings = {
    'debug': True,
    'autoreload': True,
    'static_path': '/home/path/to/pythonFile'
    }
    
    application = tornado.web.Application([\
    
                (r"(.*)",tornado.web.StaticFileHandler,\
                {"path":"static/foo/bar"}),],\
                **settings)
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()