Conexão Python MQTT ao Hub Iot do Azure

Desejo me conectar ao Azure Iot Hub, com Python MQTT.

Um nome de usuário e um token SAS são exigidos pelo Iot Hub. Este é o meu código:

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()

Mas depois de executar por um tempo, esta exceção é lançada:

TimeoutError: [WinError 10060] Uma tentativa de conexão falhou porque a parte conectada não respondeu adequadamente após um período de tempo ou a conexão estabelecida falhou porque o host conectado não respondeu

Alguém sabe por que não consigo me conectar ao Iot Hub?

questionAnswers(4)

yourAnswerToTheQuestion