How can Tornado serve one static file in any place?

I am developing a simple web application with Tornado. It serves some dynamic files and some static. Dynamic problems are not a problem, but it is difficult for me to maintain a static file. What I want to do is to serve the /path/to/foo.json file when accessing the / foo.json url.

Note that /path/to/foo.json is outside the document root. In Apache, I would just create an alias. With Tornado, I have:

app = tornado.web.Application([ (r'/dynamic\.html', MyService, dict(param = 12345)), (r'/(foo\.json)', tornado.web.StaticFileHandler, {'path': '/path/to/foo.json'}) ]) 

I added a regex group operator () to satisfy Tornado, which would otherwise throw an exception. But now, when I access /foo.json, I get 404: File not found.

Tests show that Tornado is trying to use the path provided as the root directory to which it adds foo.json, implying that my file can be found if it is in /path/to/foo.json/foo.json. Close, but not quite.

I suppose I could shorten my path to a simple "/ path / to" that will fetch /path/to/foo.json at URL / foo.json, but this forces me to use the same name in the URL address, as in the file system. How can I just make a simple, arbitrary URL to map files to?

I did some research on this subject by reading the documentation for tornado.web.Application and tornado.web.StaticFilehandler , as well as some other qaru.site/questions/808743 / ... questions . Nothing is a perfectly appropriate case.

+7
python tornado static-files
source share
3 answers

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 file name should not be related, you can do this and it will work just as well:

 app = tornado.web.Application([ (r'/jesse\.txt', MyFileHandler, {'path': '/path/to/foo.json'}) ]) 
+4
source share

StaticFileHandler expects two arguments, so if you want a single url (/foo.json) to map to your file path, you can use:

 app = tornado.web.Application([ (r'/foo.json()', tornado.web.StaticFileHandler, {'path': '/path/to/foo.json'}) ]) 

The regular expression will match /foo.json and send an empty capture group () , which will cause the file path to be used as is. When the capture group is not empty, /path/to/foo.json will be considered as the /path/to/foo.json/ directory, and the handler will try to match what is in the capture group with the file name in this directory.

+11
source share

StaticFileHandler gets the file name from the regular expression capture group and the directory name from the path argument. It will work if you use /path/to/ as the path:

 (r'/(foo\.json)', tornado.web.StaticFileHandler, {'path': '/path/to/'}) 

StaticFileHandler is designed for cases where URLs and file names match; if you cannot organize access to this file on disk under the same name that you want to serve it, since you will have to use your own handler.

+5
source share

All Articles