我的脚本应从本地IP(例如http://192.168.1.10/last_data.json
)读取json。我与requests
建立了联系,但经常遇到此错误:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
发生这种情况是因为IP http://192.168.1.10
上的微控制器正在采取措施,因此它不能用作服务器。但是,如果我等待5到10秒,则它将作为服务器恢复,因此连接将再次起作用。
我尝试了here的解决方案,但是没有用。
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
完整错误:
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',))
答案 0 :(得分:1)
肯定会执行重试逻辑,如您在粘贴的错误中所见:
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.1.10', port=80): Max retries exceeded with
我怀疑您等待主机的时间不够长,无法再次响应。每个重试请求之间的时间由following formula控制:
backoff_value = self.backoff_factor * (2 ** (self._observed_errors - 1))
因此,如果您指定backoff_factor=0.1
,它将等待0.0s
,然后依次0.2s
,然后0.4s
,依此类推,直到成功或达到最大数目为止重试。
如果您要说的话,通常需要等待5到10秒才能重试成功,那么您需要提高backoff_factor
。如果将其设置为2
,则等待时间将为0s
,2s
,4s
,8s
,16s
等。< / p>