Tornado AsyncHTTPClient URL Fetching with Error Handling

  • Share this:

Code introduction


This function uses Tornado's AsyncHTTPClient to asynchronously fetch network resources and prints the response content or error information.


Technology Stack : Tornado, AsyncHTTPClient

Code Type : Network request function

Code Difficulty : Intermediate


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

def fetch_url(url):
    client = tornado.httpclient.AsyncHTTPClient()
    def handle_response(response):
        if response.error:
            print("Error: " + response.error)
        else:
            print("Response from " + url + ": " + response.body)
    client.fetch(url, callback=handle_response)