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

前端 未结 3 1652
忘掉有多难
忘掉有多难 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:36

    StaticFileHandler gets is file name from the regex capturing group and the directory name from its 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 filenames match; if you can't arrange to have this file available on disk under the same name you want to serve it as you'll have to use a custom handler.

提交回复
热议问题