Python SDK Unable to Connect to Azure IoT Hub

Foong, Chun Sheong 31 Reputation points


I created a python module with VSCode extension (Azure IoT Edge: New Iot Edge Solution -> Python module) and tried to deploy onto an edge device I have. However, it fails to connect to IoTHub after i deploy with the error below (obtained with "sudo iotedge logs pythonmodule":

IoT Hub Client for Python
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/", line 395, in connect
    host=self._hostname, port=8883, keepalive=self._keep_alive
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/", line 941, in connect
    return self.reconnect()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/", line 1075, in reconnect
    sock = self._create_socket_connection()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/", line 3546, in _create_socket_connection
    return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
  File "/usr/local/lib/python3.7/", line 728, in create_connection
    raise err
  File "/usr/local/lib/python3.7/", line 716, in create_connection
socket.timeout: timed out

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/iothub/aio/", line 33, in handle_result
    return await callback.completion()
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/", line 94, in completion
    return await self.future
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/pipeline/", line 193, in _run_op
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/", line 417, in connect
    raise exceptions.ConnectionFailedError(cause=e)
azure.iot.device.common.transport_exceptions.ConnectionFailedError: ConnectionFailedError(None) caused by timeout('timed out')

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

Traceback (most recent call last):
  File "./", line 71, in <module>
  File "/usr/local/lib/python3.7/asyncio/", line 587, in run_until_complete
    return future.result()
  File "./", line 23, in main
    await module_client.connect()
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/aio/", line 146, in connect
    return await super(IoTHubModuleClient_, self).connect()
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/iothub/aio/", line 187, in connect
    await handle_result(callback)
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/iothub/aio/", line 37, in handle_result
    raise exceptions.ConnectionFailedError(message="Could not connect to IoTHub", cause=e)
azure.iot.device.exceptions.ConnectionFailedError: ConnectionFailedError('Could not connect to IoTHub') caused by ConnectionFailedError(None)
Unexpected error ConnectionFailedError('Could not connect to IoTHub') caused by ConnectionFailedError(None)

The code I used is the default code created with Azure IoT Extension on VSCode:

module_client = IoTHubModuleClient.create_from_edge_environment()
await module_client.connect()

What should I do to make it work?

Azure IoT Edge
Azure IoT Edge
An Azure service that is used to deploy cloud workloads to run on internet of things (IoT) edge devices via standard containers.
387 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
695 questions
1 vote

Accepted answer
  1. Satish 13,631 Reputation points Microsoft Employee

    Hello @Foong, Chun Sheong I have validated the document and was able to send the telemetry messages to the IoTHub via Python SDK.

    The below images show the telemetry being sent to IoTHub.


    The below message shows monitoring of the telemetry messages received by IoTHub.


    Please make use of the below python code, which is from file of the template code generated by the VS code+ Tutorial.


    Observation: The same error message appeared on my device logs only once and because of the device restart and then it immediately disappeared. Finally I can see telemetry flowing.

    Using commands on device side: sudo iotedge logs <pythonmodulename>

    Please comment in the below section if you need further help in this matter.

    No comments

1 additional answer

Sort by: Most helpful
  1. Foong, Chun Sheong 31 Reputation points

    Hi @Satish , yes, I verified that the issue was due to the hostname setting that I set at config.yml. I changed it to its ip address instead of hostname as I faced some TLS issue while trying to connect my Leaf console to the device; as a result that caused the error I mentioned above. Changing it back to hostname fixes the error above and I had to resort to hardcoding my hostname to IP address mapping in order to solve the TLS issue i mentioned prior. Still, thanks a lot for your help.

    No comments