Skip to content Skip to sidebar Skip to footer

Catching Sslerror Due To Unsecure Url With Requests In Python?

I have a list of a few thousand URLs and noticed one of them is throwing as SSLError when passed into requests.get(). Below is my attempt to work around this using both a solution

Solution 1:

The error I get when running your code is:

requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)

Based on this one needs to catch requests.exceptions.SSLError and not ssl.SSLError, i.e.:

try:
    response = session.get(url,allow_redirects=False,verify=True)
except requests.exceptions.SSLError:
    pass

While it looks like the error you get is different this is probably due the code you show being not exactly the code you are running. Anyway, look at the exact error message you get and figure out from this which exception exactly to catch. You might also try to catch a more general exception like this and by doing this get the exact Exception class you need to catch:

try:
    response = session.get(url,allow_redirects=False,verify=True)
except Exception as x:
    print(type(x),x)
    pass

Post a Comment for "Catching Sslerror Due To Unsecure Url With Requests In Python?"