user4093955
user4093955

Reputation:

How to request HTTP version if HTTPS failed?

I'm doing HEAD requests to a list of sites. Some of them use HTTPS and others just HTTP.

So, I'm using the following code expecting that when a SSLError is raised, a request to the HTTP version should be done. However, it looks like the error is not being caught.

for site in top:
    try:
        headers.append(requests.head('https://www.' + site.strip('\n')).headers)
    except ssl.SSLError:
        headers.append(requests.head('http://www.' + site.strip('\n')).headers)

For example, if I use Alexa Top 10, when the script reaches qq.com (top 9), the HTTPS version raises a requests.exceptions.SSLError: hostname 'www.qq.com' doesn't match either of 'a248.e.akamai.net', '*.akamaized.net', '*.akamaihd-staging.net', '*.akamaihd.net', '*.akamaized-staging.net' .

My expected use case is that if such error is raised, it should request instead http://www.qq.com.

PS: I don't want to use verify=False, I want to request the HTTP version if the HTTPS raises an error.

Upvotes: 0

Views: 161

Answers (2)

Marcus Müller
Marcus Müller

Reputation: 36352

However, it looks like the error is not being caught.

That's correct. And that happens because your code doesn't catch it!

except ssl.SSLError:

doesn't catch what requests throws – which very likely is a requests.exceptions.SSLError.

Upvotes: 2

Henry Heath
Henry Heath

Reputation: 1082

You are catching the wrong exception. It's not clear from your code what ssl.SSLError is, but it's almost certainly not the same as requests.exceptions.SSLError.

Try:

import requests

for site in top:
try:
    headers.append(requests.head('https://www.' + site.strip('\n')).headers)
except requests.exceptions.SSLError:
    headers.append(requests.head('http://www.' + site.strip('\n')).headers)

Upvotes: 1

Related Questions