2

My script should read a json from a local IP (ex http://192.168.1.10/last_data.json). I make the connection with requests but I often get this error:

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

This happens because the microcontroller on the IP http://192.168.1.10 is making a measure and so it isn't available as a server. But if I wait from 5 to 10 second, then, it will be back as a server so the connection will work again.

I tried the solution from here but it doesn't work.

import requests
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter

url = "http://192.168.1.10/last_data.json"
s = requests.Session()
retries = Retry(total=5,
                backoff_factor=0.1,
                status_forcelist=[ 500, 502, 503, 504 ])
s.mount('http://', HTTPAdapter(max_retries=retries))
response = s.get(url)

Python 3.6

The full error:

 Traceback (most recent call last):
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\util\connection.py", line 83, in create_connection
    raise err
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\util\connection.py", line 73, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py", line
601, in urlopen
    chunked=chunked)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py", line
357, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 964, in send
    self.connect()
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connection.py", line 166, in connect
    conn = self._new_conn()
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connection.py", line 150, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000024256304278>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\adapters.py", line 440,
in send
    timeout=timeout
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\connectionpool.py", line
639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\urllib3\util\retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.1.10', port=80): Max retries exceeded with
url: /data.json (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000024256304278>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\Me\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\ptvsd_launcher.py", line 45, in
<module>
    main(ptvsdArgs)
  File "c:\Users\Me\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 265, in main
    wait=args.wait)
  File "c:\Users\Me\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 256, in handle_args
    run_main(addr, name, kind, *extra, **kwargs)
  File "c:\Users\Me\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 52, in run_main
    runner(addr, name, kind == 'module', *extra, **kwargs)
  File "c:\Users\Me\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\runner.py", line 32, in run
    set_trace=False)
  File "c:\Users\Me\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "c:\Users\Me\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "c:\Users\Me\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "script.py",
line 52, in <module>
    response = s.get(url)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 521,
in get
    return self.request('GET', url, **kwargs)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 508,
in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 618,
in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\adapters.py", line 508,
in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.1.10', port=80): Max retries exceeded with url: /data.json (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000024256304278>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',))
martineau
  • 119,623
  • 25
  • 170
  • 301
MagTun
  • 5,619
  • 5
  • 63
  • 104
  • When you say it doesn't work, do you mean it doesn't make the retry requests, or it does retry five times but still ultimately fails? – cody Jan 13 '19 at 19:01
  • I think it failes before even retrying (cf my edit) – MagTun Jan 13 '19 at 19:05

1 Answers1

1

It's definitely executing the retry logic, as you can see in your pasted error:

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.1.10', port=80): Max retries exceeded with

I suspect you're not waiting long enough for the host to become responsive again. The amount of time between each retry request is governed by the following formula:

backoff_value = self.backoff_factor * (2 ** (self._observed_errors - 1))

So if you're specifying backoff_factor=0.1, it will wait 0.0s, then 0.2s, then 0.4s and so on until it either succeeds or reaches the maximum number of retries.

If you're saying you need to generally wait between 5 and 10 seconds for retries to be successful, you will need to up backoff_factor. If you set it to 2, the wait periods will then be 0s, 2s, 4s, 8s, 16s, etc.

cody
  • 11,045
  • 3
  • 21
  • 36
  • update: the formula is now `backoff_value = self.backoff_factor * (2 ** (consecutive_errors_len - 1))`. For `consecutive_errors_len` they added this comment `We want to consider only the last consecutive errors sequence (Ignore redirects)` – MagTun Jul 27 '19 at 15:19