Python Forum
Http connection error when using API request (requests library)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Http connection error when using API request (requests library)
#1
Hello. I am using requests library to make API requests from the internal server. I have noticed that every now and then maybe like (1/20) chance my program stops when I try to make a GET request and I am not sure how to solve this problem.

The error message:
Exception in Tkinter callback
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 0xafd25750>: 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='factory.teltonika.lt', port=443): Max retries exceeded with url: /gorapi/v2/279c82f4-1f45-4f4e-bdd2-944c3137d482/boxes/tree/9BE9A4CD-813C-4F48-837E-D101939F5174 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xafd25750>: 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.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/usr/lib/python3.7/tkinter/__init__.py", line 749, in callit
    func(*args)
  File "/home/pi/Desktop/lukas_programming/PTL_python/PTL_multiple_picks.py", line 718, in Scanning_operation
    Parse_GUID(user_input)
  File "/home/pi/Desktop/lukas_programming/PTL_python/PTL_multiple_picks.py", line 921, in Parse_GUID
    response_get_box_info = requests.get(URL_get_box_info)
  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='factory.teltonika.lt', port=443): Max retries exceeded with url: /gorapi/v2/279c82f4-1f45-4f4e-bdd2-944c3137d482/boxes/tree/9BE9A4CD-813C-4F48-837E-D101939F5174 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xafd25750>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
The last lines of error message indicae that the max tries are exceeded for the request. But why does it happen just occasionally and how to fix it? The request function:


def Parse_GUID(guid):
    global komplektacijos_Serial
    global komplektacijos_Imei
    global komplektacijos_ID
    global user_input  
    
    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)
    if(response_get_box_info.status_code == 200):
        parsed_guid = response_get_box_info.json()
        komplektacijos_ID = parsed_guid['Code']
        komplektacijos_Serial = parsed_guid['Products'][0]['SerialNumber']
        komplektacijos_Imei = parsed_guid['Products'][0]['Imei']
    
        user_input = komplektacijos_ID # komplektacijos_ID comes from parse_GUID
        gaminio_kodas = komplektacijos_ID+".csv"
        create_image_folder(komplektacijos_Serial,komplektacijos_ID)
        return
    else:
        time.sleep(1)
        Parse_GUID(user_input)
I know that the error appears after calling a function below:
response_get_box_info = requests.get(URL_get_box_info)
Because the debug print statement is not executed after that function. 

Any tips and ideas on what could be wrong here are appreciated! Please help me find the problem
Reply
#2
I am able to easily recreate this problem:
import requests
import time


def start_GOR_session(ID):
    global session_Name
    global session_Surname
    global session_ID
    URL_start_session = "http://factory.teltonika.lt/gorapi/v1/session/"+ID
    response_start_session = requests.get(URL_start_session)
    print("response start session=",response_start_session.status_code)
    if(response_start_session.status_code==200):
        data = response_start_session.json()
        print(data)
        session_ID = data['Id']
        session_Name = data['Name']
        session_Surname = data['Surname']
        #window.destroy()
        return
    else:
        print("cant login")




start_GOR_session("ID001025")
guid = input("prompt")  # Python 3input("Press Enter to continue...")

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)
    #response_get_box_info_curl = requests.get(URL_get_box_info)
    #if(response_start_session.status_code==200):
    print("response get box info status code=",response_get_box_info.status_code)
    time.sleep(2)
Making a request every 2 seconds. After like 10-15 requests it gives me an error:
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 0xb57bdeb0>: 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='factory.teltonika.lt', port=443): Max retries exceeded with url: /gorapi/v2/fd081c81-8060-4766-b3a4-b5a561007caa/boxes/tree/9be9a4cd-813c-4f48-837e-d101939f5174 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xb57bdeb0>: 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 31, in <module>
    response_get_box_info = requests.get(URL_get_box_info)
  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='factory.teltonika.lt', port=443): Max retries exceeded with url: /gorapi/v2/fd081c81-8060-4766-b3a4-b5a561007caa/boxes/tree/9be9a4cd-813c-4f48-837e-d101939f5174 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xb57bdeb0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Detect http request on my ip riccardoob 1 2,158 Jun-27-2021, 05:09 PM
Last Post: ndc85430
  WSAStartup error with requests yllawwally 1 2,946 Feb-23-2021, 01:53 PM
Last Post: yllawwally
  Test http requests JohnnyCoffee 2 2,340 Jul-10-2020, 03:14 AM
Last Post: JohnnyCoffee
  Error getting HTTP 200 response with requests.get while wget works fine sonicblind 0 4,960 Oct-18-2017, 02:24 PM
Last Post: sonicblind
  Configuring requests module to use secondary IP on server for outbound requests mohit 1 6,567 Oct-24-2016, 05:21 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020