話不多說,上代碼
class RedirectHandler(tornado.web.RequestHandler):
"""http轉(zhuǎn)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) # 創(chuàng)建應(yīng)用實例
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"),
})# 通過應(yīng)用實例創(chuàng)建服務(wù)器實例
https_server.listen(443) # 監(jiān)聽443端口
http_server = tornado.web.Application([(r".*", RedirectHandler)])
http_server.listen(80) # 監(jiān)聽80端口
print('start server...')
tornado.ioloop.IOLoop.instance().start() # 啟動服務(wù)器
self.request.protocol=='http'