由于请求连接尝试次数的限制,连接被拒绝

时间:2019-02-04 19:08:45

标签: python azure request azure-iot-hub

我在Azure IoT的边缘上运行了一个脚本,进行了一些计算,一旦程序计算出所需的数字,我会将其发送到两个位置:

  • 回到IoT中心
  • 通过“ request”库发布到本地网址

现在这是问题所在;经过几天的运行后,该程序停止运行,并且日志显示以下问题:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connection.py", line 159, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/util/connection.py", line 80, in create_connection
    raise err
  File "/usr/local/lib/python3.5/dist-packages/urllib3/util/connection.py", line 70, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.5/http/client.py", line 1107, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1152, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.5/http/client.py", line 1103, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/usr/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connection.py", line         181, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connection.py", line     168, in _new_conn
self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection         object at 0x6c05f6f0>: Failed to establish a new connection: [Errno 111]     Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 449, in send
timeout=timeout
    File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.5/dist-packages/urllib3/util/retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.32.61', port=1880): Max retries exceeded with url: /camara (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x6c05f6f0>: Failed to establish a new connection: [Errno 111] Connection refused',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/main.py", line 187, in <module>
    main()
  File "/app/main.py", line 161, in main
cameraCaptureSolver.start()
  File "/app/CameraCaptureSolver.py", line 161, in start
    requests.post('http://192.168.32.61:1880/camara', data = 'camaraName':camara})
  File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 116, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.32.61', port=1880): Max retries exceeded with url: /camara (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x6c05f6f0>: Failed to establish a new connection: [Errno 111] Connection refused',))

因此,问题出在请求上,但不知道重试次数有限制。

关于如何删除或增加该数字的任何想法?

2 个答案:

答案 0 :(得分:1)

这不是一个完整的答案,甚至只是建议以问题为导向。

正如您所说的“重试次数限制”,这是一种自动重试机制  的urllib3中,默认重试次数为3,如以下官方文档所述。

  

Retrying requests

     

urllib3可以自动重试幂等请求。这种相同的机制还可以处理重定向。您可以使用retries参数来请求()重试。默认情况下,urllib3将重试请求3次,并最多进行3次重定向。

您可以使用以下自定义计数设置重试次数或将其禁用,如下代码。

http.request('GET', 'http://httpbin.org/ip', retries=10) // set 10 for reties
http.request('GET', 'http://nxdomain.example.com', retries=False) // disable retrying

根据我的经验,对于错误ConnectionRefusedError,有很多可能的原因,例如:

  1. 在不稳定的网络上运行,或在不稳定的代理后面工作。
  2. 某些IoTHub限制或过期的访问令牌导致连接被拒绝的问题。对于这种情况,您需要检查状态代码和相关的错误信息,以通过打印r.status的{​​{1}}和r.data来找出原因。

您可以参考r = http.request(...)的{​​{3}}部分,以将您的详细信息与这些限制进行比较,下面还有IoT Hub limitsIoT Hub的错误代码,以及{{ 3}},这将帮助您找出真正的原因。

图1. IoT中心的常见错误 IoT Edge agent

图2. IoT Edge代理发送到IoT中心的可能响应 Reference - IoT Hub quotas and throttling

答案 1 :(得分:0)

升级AWS订阅应会增加您可以发送的请求数量。