HTTP GET Request with Timeout Handling

  • Share this:

Code introduction


This function uses urllib3's PoolManager to create an HTTP connection pool and retrieves the content from the specified URL using a GET request. If the request times out or fails, the function catches the exception and returns None.


Technology Stack : urllib3, HTTP connection pool, GET request, timeout handling

Code Type : Function

Code Difficulty : Intermediate


                
                    
import urllib3
from urllib3.util import Timeout

def fetch_url_content(url, timeout=5):
    http = urllib3.PoolManager()
    try:
        response = http.request('GET', url, timeout=Timeout(connect=timeout, read=timeout))
        return response.data
    except urllib3.exceptions.MaxRetryError as e:
        print("Failed to retrieve data from URL: {}".format(e))
        return None