We will build an asynchronous application to illustrate the functionality of Tornado. In this example, AsyncHttpClient of Tornado has been used.
Listing 4.12 explains the code for a simple network application using Tornado:
#!/usr/bin/env python # Python Network Programming Cookbook, Second Edition -- Chapter - 3 # This program is optimized for Python 2.7.12 and Python 3.5.2. # It may run on any other version with/without modifications. import argparse import tornado.ioloop import tornado.httpclient class TornadoAsync(): def handle_request(self,response): if response.error: print ("Error:", response.error) else: print (response.body) tornado.ioloop.IOLoop.instance().stop() def run_server(url): tornadoAsync = TornadoAsync() http_client = tornado.httpclient.AsyncHTTPClient() http_client.fetch(url, tornadoAsync.handle_request) tornado.ioloop.IOLoop.instance().start() if __name__ == '__main__': parser = argparse.ArgumentParser(description='Async Server Example') parser.add_argument('--url', action="store", dest="url",
type=str, required=True) given_args = parser.parse_args() url = given_args.url run_server(url)
Execute the following script to start the asynchronous application to fetch any web page. We will use it to fetch http://www.axn.com/ as follows:
$ python 14_12_tornado_async_server.py --url="http://www.axn.com/"
The following screenshot indicates the output of the execution:
Fetching axn.com Asynchronously