python iot hub_Python的MQTT连接到Azure的IOT中心

I want to connect to Azure Iot Hub, with Python MQTT.

An username and SAS token is required by Iot Hub. This is my code:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):

print("Connected with result code "+str(rc))

client.subscribe("$SYS/#")

def on_message(client, userdata, msg):

print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()

client.on_connect = on_connect

client.on_message = on_message

client.username_pw_set("myHub.azure-devices.net/device1", "mySASToken")

client.connect("myHub.azure-devices.net", 1883, 60)

client.loop_forever()

But after running for a while, this exception is thrown:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Does somebody know why I can't connect to the Iot Hub?

解决方案

As @FarukCelik said, there was no Azure IoT SDK for Python.

However, per my experience, I think there are four practicable ways using the existing SDK for Azure IoTHub in Python.

Using Azure IoT SDK for C to extending Python, you can try to refer to https://docs.python.org/2/extending/extending.html to implement it.

Using Azure IoT SDK for Java as Jython package imported, you can try to refer to http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html to know how to do it.

The similar to the second way, integrating IronPython with Azure IoT SDK for C#/.Net, please refer to http://ironpython.net/documentation/dotnet/.

The Azure IoT SDK for NodeJS support MQTT via the same Eclipse project Paho for JavaScript Client, so I think you can try to refer to the source code of Azure NodeJS IoT SDK on GitHub to know how to correctly using paho Python client for Azure IoTHub.

Meanwhile, there is a unoffical Python library for Azure IoTHub Device on GitHub https://github.com/bechynsky/AzureIoTDeviceClientPY. That you can concern about this project repository, but it's still on the deveploment stage by now.

Hope it helps. Best Regards.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值