如何在Tornado中使用HTTPS协议处理请求?

时间:2012-11-20 10:22:15

标签: https python-2.7 tornado

我是龙卷风的新手。我开始学习“Hello World"像这样的代码:

import tornado.ioloop
import tornado.web
import tornado.httpserver

class HelloHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world!")

application = tornado.web.Application([
    (r"/", HelloHandler)
])

http_server = tornado.httpserver.HTTPServer(application)

if __name__ == "__main__":
    http_server.listen(80)
    # http_server.listen(443)
    tornado.ioloop.IOLoop.instance().start()

当我进入" http:// localhost"在浏览器中,它可以工作并打印

"Hello, world!"

但如果我尝试了请求" https:// localhost",则返回:

Error 102 (net::ERR_CONNECTION_REFUSED): The server refused the connection.

有关Tornado在线的文件太少,谁能告诉我如何处理Https协议请求?

1 个答案:

答案 0 :(得分:16)

根据tornado.httpserver documentation,您需要将ssl_options字典参数传递给其构造函数,然后绑定到HTTPS端口(443):

http_server = tornado.httpserver.HTTPServer(applicaton, ssl_options={
    "certfile": os.path.join(data_dir, "mydomain.crt"),
    "keyfile": os.path.join(data_dir, "mydomain.key"),
})

http_server.listen(443)

mydomain.crt应该是您的SSL证书,mydomain.key是您的SSL私钥。