2014-05-02 28 views
0

我正在嘗試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 
+3

作爲旁分,您應該使用Paho Python客戶端而不是mosquitto.py。 Mosquitto Python代碼捐獻給了Paho,所以它是同樣的事情,但是名稱空間略有不同。修復和功能只會進入Paho。它在pypi上是paho-mqtt,或者參見http://eclipse.org/paho – ralight

回答

1

我現在不能對任何客戶端連接到broker.mqttdashboard.com:1883 - 所以這可能不是你的代碼的問題。

爲了健全性檢查,你有沒有嘗試連接到另一個經紀人,如iot.eclipse.org:1883?

+0

你是對的!我可以連接到Eclipse服務器。有什麼方法可以輕鬆查看該服務器上的客戶端發佈的消息嗎? –

+0

您可以通過其他應用程序訂閱您正在發佈的主題,並以這種方式查看消息? –