Tornado static file

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,
], **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).


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


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

python tornado favicon 的設定方法




你的電子郵件位址並不會被公開。 必要欄位標記為 *