Fetch URL Content with urllib3 and Timeout

  • Share this:

Code introduction


This function uses the urllib3 library to send a GET request to the specified URL and returns the response content. It accepts a URL and a timeout time as parameters.


Technology Stack : urllib3, HTTP requests

Code Type : Function

Code Difficulty : Intermediate


                
                    
import urllib3
from urllib3.util import Timeout

def fetch_url_content(url, timeout=5):
    """
    Fetches the content from the specified URL using urllib3 with a timeout.

    :param url: The URL from which to fetch content.
    :param timeout: The timeout in seconds for the request.
    :return: The content fetched from the URL.
    """
    http = urllib3.PoolManager()
    response = http.request('GET', url, timeout=Timeout(connect=timeout, read=timeout))
    return response.data