Tornado static file

Posted in :

Tornado 的靜態檔案,還滿好設定的,可以完全不動到 routes 裡的設定,只設“static_path” 就可以了,如果要跳出 url 裡 /static/ 才需要去動到 routes 設定值。Tornado 要效能高,似乎搭配 nginx 是主流。

 

Static files and aggressive file caching

You can serve static files from Tornado by specifying the static_path setting in your application:

settings = {
    "static_path": os.path.join(os.path.dirname(__file__), "static"),
    "cookie_secret": "__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
    "login_url": "/login",
    "xsrf_cookies": True,
}
application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/login", LoginHandler),
    (r"/(apple-touch-icon\.png)", tornado.web.StaticFileHandler,
     dict(path=settings['static_path'])),
], **settings)

This setting will automatically make all requests that start with /static/ serve from that static directory, e.g., http://localhost:8888/static/foo.png will serve the file foo.png from the specified static directory. We also automatically serve /robots.txt and /favicon.ico from the static directory (even though they don’t start with the /static/ prefix).

from:
http://www.tornadoweb.org/en/stable/guide/running.html


實際上 class StaticFileHandler(RequestHandler): 的 source code 及用法:

https://github.com/tornadoweb/tornado/blob/master/tornado/web.py

 


如果沒有搭配nginx 處理 favicon.ico 的解法:

python tornado favicon 的設定方法
http://stackoverflow.max-everyday.com/2017/09/python-tornado-favicon/

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *