简体   繁体   中英

response = requests.get error when URL is not reachable

I am trying to write a script and part of it has to check if a URL is available or not. The problem is that when I get a reply back 200,404, etc the program works fine and I can handle the reply but when the URL is not reachable, the program goes into the error below. This is part of the code:

response = requests.get(url)
print (response)

Error:

Traceback (most recent call last):
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "C:\Python\lib\site-packages\requests\packages\urllib3\util\connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "C:\Python\lib\socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 345, in _make_request
    self._validate_conn(conn)
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 844, in _validate_conn
    conn.connect()
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 284, in connect
    conn = self._new_conn()
  File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 150, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x03EC9970>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

Is there a workaround to this? If I can set the script to print a line like URL not available and exists will be great.

Just catch the exception . The Errors and Exceptions section of the requests documentation indicates you should be able to catch the requests.ConnectionError exception here:

try:
    response = requests.get(url)
except requests.ConnectionError:
    print("Can't connect to the site, sorry")
else:
    print(response)

Quick demo with a non-existing hostname:

>>> import requests
>>> try:
...     response = requests.get("http://no_such_site_exists.imsure")
... except requests.ConnectionError:
...     print("Can't connect to the site, sorry")
... else:
...     print(response)
...
Can't connect to the site, sorry

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM