Introduction to Tornado HTTP client&server module
Introduction
Tornado is an open-source version of the scalable, non-blocking web server and tools that power FriendFeed. One of the powerful features of Tornado is its ability to manage HTTP clients and servers. This article aims to provide a comprehensive understanding of these modules for beginners.
The Basics
Tornado HTTP Server
Tornado's HTTP server is an HTTP/1.1 implementation using a non-blocking network I/O. This is advantageous because it enables handling of multiple connections simultaneously, thus, improving the performance of the server. Here's how to get started:
import tornado.httpserver
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(8888)
tornado.ioloop.IOLoop.current().start()
In the above example, we create a basic HTTP server, which listens on port 8888 and responds with "Hello, world" to get requests at the root URL ("/").
Tornado HTTP Client
The Async HTTP client implemented in Tornado is capable of handling multiple connections. The asynchronous nature allows it to make non-blocking HTTP requests.
Here's a basic example:
from tornado import httpclient
def handle_response(response):
if response.error:
print("Error: %s" % response.error)
else:
print(response.body)
http_client = httpclient.AsyncHTTPClient()
http_client.fetch("http://www.google.com", handle_response)
In this example, we make a non-blocking HTTP request to google.com. The response is handled by the handle_response
function.
Details
HTTP Server Options
The Tornado HTTP server has several options for customization. For example, you can specify the number of threads to use with the max_buffer_size
argument:
http_server = tornado.httpserver.HTTPServer(app, max_buffer_size=50485760)
HTTP Client Options
Similarly, the HTTP client also has various options. For instance, you can set the request timeout as follows:
http_client = httpclient.AsyncHTTPClient(defaults=dict(request_timeout=20.0))
HTTPServer Request Callback
When a request comes into the Tornado HTTP server, it is given to a callback function. This function is responsible for handling the request. In the basic server example above, the MainHandler
class is the request callback.
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
HTTP Client Response Handling
When the HTTP client receives a response, it is handled by a function you define. In the basic client example, the handle_response
function is used.
def handle_response(response):
if response.error:
print("Error: %s" % response.error)
else:
print(response.body)
Conclusion
Tornado's HTTP client and server modules are powerful tools for creating highly scalable and efficient web applications. Understanding these modules will give you a solid foundation in creating your own Tornado applications. Happy coding!