Reputation: 1717
I am trying to implement program that determine, if page support tls or not and if it is needed to have prefix www
. So I am testing page1.cz and check response status of this objects:
Session().get('http://page1.cz')
<Response [200]>
Session().get('http://www.page1.cz')
<Response [200]>
Session().get('https://page1.cz')
<Response [200]>
Session().get('https://www.page1.cz')
<Response [200]>
It works fine, I know that page1.cz is using https and it is always redirect to https://page1.cz
. When I tried page2.cz, I recieved error when testing with https
prefix. I receiving this error:
Session().get('http://page2.cz')
<Response [200]>
Session().get('http://www.page2.cz')
<Response [200]>
Session().get('https://page2.cz')
ConnectionError: HTTPSConnectionPool(host='page2.cz', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f75e85f03c8>: Failed to establish a new connection: [Errno 111] Connection refused',))
Session().get('https://www.page2.cz')
ConnectionError: HTTPSConnectionPool(host='www.page2.cz', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f75e85f03c8>: Failed to establish a new connection: [Errno 111] Connection refused',))
I know that second page does not support https
but why that error? It should just return code 4xx or am I wrong? What am I doing wrong and how to check if page support http
, https
and www
prefixes?
Upvotes: 0
Views: 2672
Reputation: 11929
The error says that the host refused the connection and an error is raised.
You can handle the exception using a try-except
block.
import requests
try:
req = requests.get(your_website)
except requests.exceptions.ConnectionError:
print("Connection refused")
Additionally you can set a timeout for the request, e.g.,
req = requests.get(your_website, timeout=1)
Consider for instance the following website http://www.qq.com/ that does not support https.
With your_website
being http://www.qq.com/
you would receive a 200 OK, while with your_website
being https://www.qq.com/
an exception is raised.
Upvotes: 1