Using urllib3#s PoolManager for HTTP GET Requests

  • Share this:

Code introduction


This function uses urllib3's PoolManager to send a GET request to the specified URL and returns the response data. If an HTTP error occurs, it returns the error information.


Technology Stack : urllib3, PoolManager, HTTPError

Code Type : Function

Code Difficulty : Intermediate


                
                    
def fetch_random_url(url):
    from urllib3 import PoolManager, HTTPError

    try:
        with PoolManager() as http:
            response = http.request('GET', url)
            return response.data
    except HTTPError as e:
        return f"HTTP Error: {e.code} - {e.reason}"