Tornado HTTPClient URL Fetch Function

  • Share this:

Code introduction


This code defines a function to fetch a URL using Tornado's HTTPClient and prints the response or an error message.


Technology Stack : Tornado, HTTPClient

Code Type : Web Server and HTTP Client

Code Difficulty : Intermediate


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

def fetch_url(url):
    def on_fetch(response):
        if response.error:
            print("Error fetching URL: {}".format(response.error))
        else:
            print("Response from URL: {}".format(response.body))

    http_client = tornado.httpclient.HTTPClient()
    http_client.fetch(url, on_fetch)