如何为 Tornado 服务器上的静态文件自定义 header?

How can I customize header for static file on tornado server?

当我请求位于静态目录的 favicon.ico 时,龙卷风服务器响应服务器 Header(服务器:TornadoServer/4.4.2)。 我想隐藏服务器名称和版本。我该如何修改它?

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/", HomeHandler),
            (r".*", BaseHandler),
        ]
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            debug=True,
        )

我可以通过这个隐藏服务器 Header 在正常内容上。

class BaseHandler(tornado.web.RequestHandler):
    @property
    def set_default_headers(self):
        self.set_header("Server", "hidden")

Tornado 允许使用 settings

中的 static_handlr_class 选项更改默认静态处理程序 class
import os
import tornado.ioloop
import tornado.web


class HomeHandler(tornado.web.RequestHandler):

    def get(self):
        self.write('ok')

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/", HomeHandler),
        ]
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            static_handler_class=MyStaticFileHandler,
            debug=True,
        )
        super(Application, self).__init__(handlers, **settings)

class MyStaticFileHandler(tornado.web.StaticFileHandler):

    def set_default_headers(self):
        self.set_header("Server", "hidden")

if __name__ == "__main__":
    application = Application()
    application.listen(8888)
    tornado.ioloop.IOLoop.current().start()