You can download this code by clicking the button below.
This code is now available for download.
This function uses the urllib3 library to send an HTTP GET request to the specified URL and returns the response content. If the request fails, it returns None. The function also includes handling HTTP errors and other exceptions.
Technology Stack : urllib3, Python
Code Type : Function
Code Difficulty : Intermediate
import urllib3
from urllib3.util import Timeout
from urllib3.exceptions import HTTPError
def fetch_url_content(url, timeout=5):
"""
Fetches the content from the given URL using urllib3 with a specified timeout.
:param url: The URL to fetch the content from.
:param timeout: The timeout in seconds for the request.
:return: The response content if the request is successful, otherwise None.
"""
http = urllib3.PoolManager()
try:
response = http.request('GET', url, timeout=Timeout(connect=timeout, read=timeout))
return response.data
except HTTPError as e:
print(f"HTTP error occurred: {e}")
return None
except Exception as e:
print(f"An error occurred: {e}")
return None