Tornado HTTPClient Data Fetch Function

  • Share this:

Code introduction


This function uses the httpclient module from the Tornado library to fetch data from a specified URL and prints the response content if successful. If an error occurs, it will print the error information.


Technology Stack : Tornado, httpclient

Code Type : Function

Code Difficulty : Intermediate


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

def fetch_random_data(url):
    def handle_response(response):
        if response.error:
            print("Error fetching URL:", response.error)
        else:
            print("Response from URL:", response.body)
    
    http_client = tornado.httpclient.HTTPClient()
    http_client.fetch(url, callback=handle_response)