Random URL Fetch with Tornado HTTP Client

  • Share this:

Code introduction


This function uses Tornado's HTTP client to randomly access a predefined list of URLs and prints out the response content.


Technology Stack : Tornado, HTTP client

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import tornado.ioloop
import tornado.web
import tornado.httpclient

def fetch_random_url():
    """
    This function fetches a random URL and prints the response.
    It uses Tornado's HTTP client to make the request.
    """
    urls = ["http://example.com", "http://python.org", "http://tornado.io", "http://github.com"]
    client = tornado.httpclient.HTTPClient()
    url = random.choice(urls)
    response = client.fetch(url)
    print(response.body)