Leonard
Leonard

Reputation: 139

How to handle a request with HTTPS protocol in Tornado?

I'm a newbie in Tornado. And I begin my learning with “Hello World" code like this:

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()

When I entered "http://localhost" at the browser, it works and prints

"Hello, world!"

But if I tried the request "https://localhost", it returns with:

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

There are too little documents about Tornado online, who can tell me how to deal with Https protocol request?

Upvotes: 11

Views: 8305

Answers (1)

Mickaël Le Baillif
Mickaël Le Baillif

Reputation: 2157

According to tornado.httpserver documentation, you need to pass ssl_options dictionary argument to its constructor, then bind to the HTTPS port (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 should be your SSL certificate, and mydomain.key your SSL private key.

Upvotes: 18

Related Questions