How can Tornado serve a single static file at an arbitrary location?

前端 未结 3 1644
忘掉有多难
忘掉有多难 2021-02-14 12:50

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

3条回答
  •  执念已碎
    2021-02-14 13:34

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

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

    The regex will match /foo.json and send the empty capture group (), which will cause the filepath to be used as is. When the capture group is not empty, /path/to/foo.json will be treated as a directory /path/to/foo.json/, and the handler will try to match whatever is within the capture group to a file name in that directory.

提交回复
热议问题