You can download this code by clicking the button below.
This code is now available for download.
This code example creates a simple Tornado web application, including an asynchronous HTTP client to fetch responses from a random URL, as well as a basic HTTP request handler.
Technology Stack : Tornado, HTTPClient
Code Type : Tornado Web Application
Code Difficulty : Intermediate
import tornado.ioloop
import tornado.web
import tornado.httpclient
import random
def fetch_random_url():
urls = [
"https://www.google.com",
"https://www.bing.com",
"https://www.wikipedia.org",
"https://www.reddit.com"
]
client = tornado.httpclient.AsyncHTTPClient()
url = random.choice(urls)
def handle_response(response):
print("Response from", url, "status:", response.code)
client.fetch(url, handle_response)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])