Search code examples
pythonmqttazure-iot-hubazure-iot-edgeazure-iot-sdk

Azure IoT client not getting connected to IoT Hub


Im facing these errors in my Invoke Module. I have two modules which runs python script. One module to get invoked cloud message and other to receive the invoked cloud message through the input. Everything was working fine and suddenly Im receiving:

FYI: The python script with my updated changes is working fine when I commit changes after entering into the container(which has my previously successfully build and deployed version of module), but when I deploy the module through the deployment manifest its failing with these errors.

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 

Im using azure-iot-device = 2.1.1.

Please help me with suggestion to resolve this.


Solution

  • I have faced a similar issue and I resolved it using the following. Please check if you have mentioned

    NetworkMode = "Hosts" in your deployment.json

    If yes then remove it and it works just fine