我的脚本应该从本地IP读取JSON(例如
http://192.168.1.10/last_data.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',))