Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 275
Leonard Avatar asked Nov 20 '12 10:11

Leonard


People also ask

How many requests can tornado handle?

Python Tornado Request Performance.md Meaning tornado will be able to handle a maximum of 10 simultaneous fetch() operations in parallel on each IOLoop. This means a single tornado process should only be able to handle ~3 incoming requests before subsequent ones would queue in Tornado's AsyncHTTPClient.

What is Tornado API?

Tornado is a Python web framework and asynchronous network library, originally developed at FriendFreed. Tornado uses non-blocking network-io. Due to this, it can handle thousands of active server connections. It is a saviour for applications where long polling and a large number of active connections are maintained.

What is Tornado web application?

A Tornado web application generally consists of one or more RequestHandler subclasses, an Application object which routes incoming requests to handlers, and a main() function to start the server.

How do you stop a tornado server?

1. Just `kill -2 PROCESS_ID` or `kill -15 PROCESS_ID` , The Tornado Web Server Will shutdown after process all the request.


1 Answers

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.

like image 95
Mickaël Le Baillif Avatar answered Oct 16 '22 01:10

Mickaël Le Baillif