總之,我有一個Paho/MQTT示例集可以正常工作,但我在檢測錯誤時遇到了問題。具體來說,我沒有獲得on_connect回調,並且當給出無效的UserID/Pswd組合時,它會自動失敗。事實上,每一個跡象都表明它一切正常!Python Paho/MQTT:檢測連接錯誤
我在做什麼錯?
(snip)
def on_connect(client, userdata, flags, rc):
print("Connected with flags [%s] rtn code [%d]"% (flags, rc))
def on_disconnect(client, userdata, rc):
print("disconnected with rtn code [%d]"% (rc))
def on_publish(client, userdata, msgID):
print("Published with MsgID [%d]"% (msgID))
mqttc = mqtt.Client()
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.on_publish = on_publish
mqttc.username_pw_set(Q_USER, Q_PSWD)
rc=mqttc.connect(Q_BROKER, Q_PORT)
print "Return="+str(rc)
mqttc.loop_start()
rc=mqttc.publish(Q_TOPIC, "Hello, World!")
print "Return="+str(rc)
mqttc.disconnect()
mqttc.loop_stop()
輸出時,用戶名或PSWD是故意錯誤:
Return=0
Published with MsgID [1]
Return=(0, 1)
disconnected with rtn code [0]
添加到混淆......如果我在connect()調用之前移動loop_start(),則郵件無法正確發送。仍然沒有錯誤跡象 – Jeppedy
您正在使用哪個代理,您是如何配置身份驗證的?例如如果mosquitto是'allow_anonymous'是真的? – hardillb
請注意,connect()調用之前的loop_start()會導致未定義的結果。 – Jeppedy