Python Requests throwing SSLError
Here's a code snippet that demonstrates how to use the Python requests
library to make a GET request to a URL, while handling a possible SSLError
:
import requests
from requests.exceptions import SSLError
url = "https://example.com"
try:
response = requests.get(url)
# Do something with the response
print(response.content)
except SSLError as e:
print("An SSL error occurred:", e)
except Exception as e:
print("An error occurred:", e)
In this example, the requests.get(url)
method is used to make a GET request to the specified URL. If an SSLError
is raised, the code in the except SSLError
block will be executed and it will print the error message.
Watch a video course
Python - The Practical Guide
It is also common to use requests.get(url, verify=False)
instead, this would ignore the ssl certification error but it should be used only in development and testing, not in production.