我正在嘗試MQTT第一次使用Python和蚊子庫。我的客戶程序在下面。我試圖在http://www.mqtt-dashboard.com/subscribe上使用公開演示MQTT服務器。但客戶端代碼失敗,請參閱下面的錯誤。關於發生什麼事的任何想法?爲什麼此MQTT客戶端示例失敗?
#!/usr/bin/env python
import mosquitto
client = mosquitto.Mosquitto("fredtest", clean_session=True)
client.connect("broker.mqttdashboard.com", 1883)
client.publish("fred.test", "hello world", 1)
client.loop_forever()
錯誤消息:
C:\tmp>python mqttclient.py Traceback (most recent call last): File "mqttclient.py", line 6, in client.connect("broker.mqttdashboard.com", 1883) File "build\bdist.win-amd64\egg\mosquitto.py", line 582, in connect File "build\bdist.win-amd64\egg\mosquitto.py", line 657, in reconnect File "c:\python27\lib\socket.py", line 571, in create_connection raise err socket.error: [Errno 10060] A connection attempt failed because the connected pa rty did not properly respond after a period of time, or established connection f ailed because connected host has failed to respond
作爲旁分,您應該使用Paho Python客戶端而不是mosquitto.py。 Mosquitto Python代碼捐獻給了Paho,所以它是同樣的事情,但是名稱空間略有不同。修復和功能只會進入Paho。它在pypi上是paho-mqtt,或者參見http://eclipse.org/paho – ralight