Requests library failure in name resolution error

Thread Starter

zazas321

Joined Nov 29, 2015
716
Hello. I am using Requests library to make API requets to the internal server/database.
I am making requests as following:
Code:
while(1):
    URL_get_box_info = "https://factory.teltonika.lt/gorapi/v2/"+session_ID+"/boxes/tree/"+guid
    response_get_box_info = requests.get(URL_get_box_info)
    print("response get box info status code=",response_get_box_info.status_code)
    time.sleep(2)
And everything seemed fine at first glance. However, as I was testing the system more and more, I have realized that ocassionally, evey 10,15 requets I am getting a "failure in name resolution" or "Connection refused" error and cannot understand what the problem is..

See the images below:
2020-10-23-073125_1920x1080_scrot.png2020-10-23-073042_1920x1080_scrot.png

I then went ahead and tried to making a request to https:/google.com, but the same problem apperas:

Code:
while(1):
    response_get_box_info = requests.get("https://google.com")
    print("response get box info status code=",response_get_box_info.status_code)
    time.sleep(2)
THe full error message:
Code:
response get box info status code= 200
response get box info status code= 200
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 159, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 57, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 841, in _validate_conn
    conn.connect()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 301, in connect
    conn = self._new_conn()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 168, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0xb580ee50>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xb580ee50>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/Desktop/lukas_programming/PTL_python/api_test.py", line 35, in <module>
    response_get_box_info = requests.get("https://google.com")
  File "/usr/lib/python3/dist-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xb580ee50>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
Just to note, when I just simply go to browser and type API url, I am getting the correct response at all times, so it makes me think that it has to do with the requests library? I really do not know how to even start debugging this problem. I have read a few forums regarding this issue but none that would solve this
 

Thread Starter

zazas321

Joined Nov 29, 2015
716
I have connected to the same WIFI and ran the same python script on my laptop. No problems whatsoever. So the issue is not with requests library but with the raspberry PI.
 

WBahn

Joined Mar 31, 2012
26,301
I have never used that library, so I'm shooting from the hip. It says that it is a temporary error. Makes me think that the issue is that occasionally when you make the request the server can't process it for some reason and so you are getting this error. IF that is what is happening, then you probably want to catch the exception that is being thrown and keep making the request until it is successful (or handle it some other way).
 

Thread Starter

zazas321

Joined Nov 29, 2015
716
I have never used that library, so I'm shooting from the hip. It says that it is a temporary error. Makes me think that the issue is that occasionally when you make the request the server can't process it for some reason and so you are getting this error. IF that is what is happening, then you probably want to catch the exception that is being thrown and keep making the request until it is successful (or handle it some other way).
Thanks very much for pointing me in the right direction!!
Code:
try:
      response_get_box_info = requests.get("https://google.com")
except requests.exceptions.HTTPError as errh:
    print ("Http Error:",errh)
except requests.exceptions.ConnectionError as errc:
    print ("Error Connecting:",errc)
except requests.exceptions.Timeout as errt:
    print ("Timeout Error:",errt)
works like supposed to now!
 
Top