I am developing a simple web app with Tornado. It serves some dynamic files and some static ones. The dynamic ones are not a problem, but I am having trouble serving a static fi
Something like this should work:
import os
import tornado.ioloop
import tornado.web
class MyFileHandler(tornado.web.StaticFileHandler):
def initialize(self, path):
self.dirname, self.filename = os.path.split(path)
super(MyFileHandler, self).initialize(self.dirname)
def get(self, path=None, include_body=True):
# Ignore 'path'.
super(MyFileHandler, self).get(self.filename, include_body)
app = tornado.web.Application([
(r'/foo\.json', MyFileHandler, {'path': '/path/to/foo.json'})
])
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
The URL pattern and the filename need not be related, you could do this and it would work just as well:
app = tornado.web.Application([
(r'/jesse\.txt', MyFileHandler, {'path': '/path/to/foo.json'})
])