Azure IoT 客户端未连接到 IoT 中心

Azure IoT client not getting connected to IoT Hub

我在调用模块中遇到了这些错误。 我有两个运行 python 脚本的模块。一个模块获取调用的云消息,另一个模块通过输入接收调用的云消息。 一切正常,我突然收到:

仅供参考:当我进入容器(其中有我之前成功构建和部署的模块版本)后提交更改时,带有我更新的更改的 python 脚本工作正常,但是当我部署模块时通过部署清单,它因这些错误而失败。

Connected with result code 0
transport.connect raised error
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/mqtt_transport.py", line 383, in connect
    host=self._hostname, port=8883, keepalive=DEFAULT_KEEPALIVE
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 937, in connect
    return self.reconnect()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1071, in reconnect
    sock = self._create_socket_connection()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3522, in _create_socket_connection
    return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
  File "/usr/local/lib/python3.7/socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/local/lib/python3.7/socket.py", line 752, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/pipeline/pipeline_stages_mqtt.py", line 168, in _run_op
    self.transport.connect(password=self.sas_token)
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/mqtt_transport.py", line 405, in connect
    raise exceptions.ConnectionFailedError(cause=e)
azure.iot.device.common.transport_exceptions.ConnectionFailedError: ConnectionFailedError(None) caused by gaierror(-2, 'Name or service not known')

ConnectOperation: completing with error ConnectionFailedError(None) caused by gaierror(-2, 'Name or service not known')
ConnectionLockStage(ConnectOperation): op failed.  Unblocking queue with error: ConnectionFailedError(None) caused by gaierror(-2, 'Name or service not known')
ConnectOperation: completing with error ConnectionFailedError(None) caused by gaierror(-2, 'Name or service not known')
Callback completed with error ConnectionFailedError(None) caused by gaierror(-2, 'Name or service not known')
["azure.iot.device.common.transport_exceptions.ConnectionFailedError: ConnectionFailedError(None) caused by gaierror(-2, 'Name or service not known')\n"]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/mqtt_transport.py", line 383, in connect
    host=self._hostname, port=8883, keepalive=DEFAULT_KEEPALIVE 

我正在使用 azure-iot-device = 2.1.1.

请帮我提出解决这个问题的建议。

粗略地扫描堆栈表明您可能遇到了 DNS 问题。似乎无法解析主机名。这就是 getaddrinfo 试图做的。你没有提到这是什么类型的设备,但是,如果你有可用的 ping,请尝试使用它来 ping 你的 IoT 中心。它不会响应 ping,但会告诉您它是否能够解析名称。

我遇到过类似的问题,我使用以下方法解决了它。 请检查您是否提到

您的 deployment.json

中的

NetworkMode = "Hosts"

如果是,则删除它,它就可以正常工作