2017-04-12 148 views
0

我用下面的代碼,我得到這個錯誤:MQTT連接建立:

Time out error : [WinError 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 failed to respond. 

我也關閉了防火牆的系統我使用,我仍然得到那個錯誤。

#!/usr/bin/env python3 

import paho.mqtt.client as mqtt 

# This is the Publisher 

client = mqtt.Client() 
client.connect("10.12.114.103",1883,60) 
client.publish("topic/test", "Hello world!"); 
client.disconnect(); 
+1

的MQTT經紀人@ 10.12.114.103:1883沒有應答,請嘗試使用MQTTfx或類似工具連接到檢查的經紀人正在 – Adirio

+0

我用hivemq經紀人,仍然會得到相同的錯誤.. – Stacy

+0

您確定該經紀人在您運行測試時在10.12.114.103上運行嗎? – hardillb

回答

0

的問題可能不是與你的Python代碼,我試圖與hivemq的公開測試服務器上稍作修改的版本,它工作得很好。這裏是我試過的版本:

#!/usr/bin/env python3 

import paho.mqtt.client as mqtt 

# This is the Publisher 

def on_log(client, userdata, level, buf): 
    print(level, buf) 

client = mqtt.Client() 
client.on_log = on_log 
client.connect("broker.hivemq.com",1883,60) 
client.publish("topic/test", "Hello world!"); 
client.disconnect(); 

請注意,我修改它以返回日誌輸出,這將有助於調試。該腳本返回:

16 Sending PUBLISH (dFalse, q0, r0, m1, 'topic/test', ... (12 bytes) 

這是我期望從一個成功的連接。我還檢查了您的代碼與蚊子測試服務器,它工作正常。

看起來你的經紀人不接受你的連接嘗試。如果您嘗試了一個公共測試服務器,但它仍然無法正常工作,則表明有一些干擾您端口1883上的流量。

我認爲這是一個經紀人問題,但是我注意到您並未使用根據paho文檔,任何網絡循環函數(如client.loop_start)都可能導致不可預知的行爲。你可以嘗試添加這樣一個循環來看看是否有幫助

client = mqtt.Client() 
client.on_log = on_log 
client.connect("broker.hivemq.com",1883,60) 
client.loop_start 
client.publish("topic/test", "Hello world!"); 
client.disconnect(); 
client.loop_stop 
+0

我得到一個連接拒絕錯誤 - 「無法建立連接,因爲目標機器主動拒絕它」 – Stacy

+0

我對該錯誤的理解是,代理根本不接受連接。您是否與其中一名測試經紀人一起嘗試了示例代碼?它有用嗎? – blp

+0

我嘗試了「broker.hivemq.com」,爲此我得到了上述錯誤。但如果我與同一個本地主機中的經紀人連接,它正在連接.. – Stacy