tornado http转https

话不多说,上代码

class RedirectHandler(tornado.web.RequestHandler):
    """http转https"""
    def prepare(self):
        self.redirect(url='https://' + self.request.host + self.request.uri)


if __name__ == "__main__":
    app = tornado.web.Application(handlers, **cfg.init.web_settings)  # 创建应用实例
    https_server = tornado.httpserver.HTTPServer(app, ssl_options={
           "certfile": os.path.join(os.path.abspath("/etc/letsencrypt/live/test.ahotfund.com/"), "fullchain.pem"),
           "keyfile": os.path.join(os.path.abspath("/etc/letsencrypt/live/test.ahotfund.com/"), "privkey.pem"),
    })# 通过应用实例创建服务器实例
    https_server.listen(443)  # 监听443端口
    http_server = tornado.web.Application([(r".*", RedirectHandler)])
    http_server.listen(80)  # 监听80端口
    print('start server...')
    tornado.ioloop.IOLoop.instance().start()  # 启动服务器

self.request.protocol=='http'

你可能感兴趣的:(tornado http转https)