如何从静态路径以外的其他目录提供静态文件?

shi*_*ino 32 python favicon static tornado

我在尝试这个:

favicon_path = '/path/to/favicon.ico'

settings = {'debug': True, 
            'static_path': os.path.join(PATH, 'static')}

handlers = [(r'/', WebHandler),
            (r'/favicon.ico', tornado.web.StaticFileHandler, {'path': favicon_path})]

application = tornado.web.Application(handlers, **settings)
application.listen(port)
tornado.ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)

但是它一直favicon.ico在我的static_path中提供服务(我在两个不同favicon.ico的路径中有两个不同的路径,如上所示,但我希望能够覆盖它中的那个static_path).

Not*_*fer 52

static_path从应用设置中删除.

然后设置你的处理程序:

handlers = [
            (r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': favicon_path_dir}),
            (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': static_path_dir}),
            (r'/', WebHandler)
]
Run Code Online (Sandbox Code Playgroud)

  • @shino它有效,因为r'/ favicon.ico'是一个正则表达式,你正确地逃脱了'.' (8认同)
  • 似乎在应用程序设置中设置static_path对于favicon和robots.txt有一个特殊情况.来自文档:`我们将从同一[static_path]目录中提供/favicon.ico和/robots.txt (5认同)

use*_*508 6

您需要用括号括起favicon.ico并在正则表达式中转义句点.你的代码将成为

favicon_path = '/path/to/favicon.ico' # Actually the directory containing the favicon.ico file

settings = {
    'debug': True, 
    'static_path': os.path.join(PATH, 'static')}

handlers = [
    (r'/', WebHandler),
    (r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': favicon_path})]

application = tornado.web.Application(handlers, **settings)
application.listen(port)
tornado.ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)