Python 使用Paho库通过MQTT协议向Azure IOT hub发送消息

Python 使用Paho库通过MQTT协议向Azure IOT hub发送消息,python,mqtt,azure-iot-hub,paho,Python,Mqtt,Azure Iot Hub,Paho,大家好, 我正在尝试通过MQTT协议(使用Python)通过Paho库向Azure IOT hub发送遥测消息。我提到 (Microsoft Docs)并遵循与该文档完全相同的步骤。但是,我在运行代码时遇到了一个错误 下面是python代码 from paho.mqtt import client as mqtt import ssl path_to_root_cert = "C:\Python_Files\Digicert_Cert.txt" device_id = "MyDeviceNam

大家好,

我正在尝试通过MQTT协议(使用Python)通过Paho库向Azure IOT hub发送遥测消息。我提到 (Microsoft Docs)并遵循与该文档完全相同的步骤。但是,我在运行代码时遇到了一个错误

下面是python代码

from paho.mqtt import client as mqtt
import ssl

path_to_root_cert = "C:\Python_Files\Digicert_Cert.txt"
device_id = "MyDeviceName"
sas_token = "SharedAccessSignature sr=MyHubName.azure-devices.net&sig=UclWeYtF5WSy4QUvTQvDF1ml2fVze0VFpv4e7YLFdQE%3D&se=1567761926&skn=iothubowner"
iot_hub_name = "MyHubName"

def on_connect(client, userdata, flags, rc):
  print ("Device connected with result code: " + str(rc))
def on_disconnect(client, userdata, rc):
  print ("Device disconnected with result code: " + str(rc))
def on_publish(client, userdata, mid):
  print ("Device sent message")

client = mqtt.Client(client_id=device_id, protocol=mqtt.MQTTv311)

client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish

client.username_pw_set(username=iot_hub_name+".azure-devices.net/" + device_id, password=sas_token)

client.tls_set(ca_certs=path_to_root_cert, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None)
client.tls_insecure_set(False)

client.connect(iot_hub_name+".azure-devices.net", port=8883)

client.publish("devices/" + device_id + "/messages/events/", "{id=123}", qos=1)
client.loop_forever()
我在运行python代码时遇到以下错误

Traceback (most recent call last):
  File "C:\Python_Files\Python Script.py", line 27, in <module>
    client.connect(iot_hub_name+".azure-devices.net", port=8883)
  File "C:\Python27\lib\site-packages\paho\mqtt\client.py", line 839, in connect
    return self.reconnect()
  File "C:\Python27\lib\site-packages\paho\mqtt\client.py", line 962, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
  File "C:\Python27\lib\socket.py", line 575, in create_connection
    raise err
error: [Errno 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.

任何帮助都将不胜感激。提前感谢。

您的帖子中的
sas_令牌
似乎不正确,它不包含
%2F设备%2F{deviceid}
。我不知道您是如何获得令牌的,但我认为您可以使用该工具生成SAS令牌


谢谢迈克尔抽出时间。我只使用设备资源管理器工具生成SAS令牌。我已经用设备资源管理器生成SAS令牌的快照更新了我的问题。@HarshadHolkar,当您使用设备资源管理器生成SAS令牌时,您确定您选择的设备id与代码中的设备id相同吗?请检查一下。代码对我来说很好。是的,Michael,我在代码中使用了相同的设备id,并且在从设备资源管理器生成SAS令牌时使用了相同的设备id。您是否检查了防火墙和打开的端口?你的网络在代理服务器中吗?是的,Michael防火墙是问题所在。。。非常感谢你帮我弄明白这一点。
sas_token ="SharedAccessSignature sr=PuneODCIOTHub.azure-devices.net%2Fdevices%2FMyDotnetDevice&sig=KqyeH0n2kez3Zyz3%2BnVnOVyAsG%2F65MYO95%2FrgdJjBzI%3D&se=1536300480"