如何为tornado服务器上的静态文件自定义标头?

时间:2017-07-24 03:08:33

标签: python-2.7 tornado

当我向位于静态目录上的favicon.ico请求时,龙卷风服务器响应Server Header(Server: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,
        )

我可以通过此隐藏正常内容上的服务器标头。

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

1 个答案:

答案 0 :(得分:0)

Tornado允许使用settings

中的static_handlr_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()