2014-10-28 71 views
2

我試圖使用python 2.7連接到消息代理在raspbian如下:在樹莓派使用MQTT泛美衛生組織爲Python

import paho.mqtt.client as paho 


host="messagesight.demos.ibm.com" 
port=1883 

def on_connect(pahoClient, obj, rc): 
# Once connected, publish message 
     print "Connected Code = %d"%(rc) 
     client.publish("prueba/123", "Hello World", 0) 


def on_log(pahoClient, obj, level, string): 
     print string 

def on_publish(pahoClient, packet, mid): 
# Once published, disconnect 
     print "Published" 
     pahoClient.disconnect() 

def on_disconnect(pahoClient, obj, rc): 
     print "Disconnected" 

# Create a client instance 
client=paho.Client() 

# Register callbacks 
client.on_connect = on_connect 
client.on_log = on_log 
client.on_publish = on_publish 
client.on_disconnnect = on_disconnect 

#Set userid and password 
client.username_pw_set(userID, password) 

#connect 
x = client.connect(host, port, 60) 

client.loop_forever() 

當我運行該腳本,即時得到以下錯誤:

Traceback (most recent call last): File "ejemplo.py", line 27, in client=paho.Client() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 410, in init self._sockpairR, self._sockpairW = _socketpair_compat() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 255, in _socketpair_compat listensock.bind(("localhost", 0)) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 99] Cannot assign requested address

我該如何解決?

+2

寫這個感覺有點奇怪,但是你有沒有在你的Pi上支持IPv4? – ralight 2014-10-29 16:51:07

+2

或者您的「lo」界面關閉。嘗試'ifconfig lo down; ifconfig lo up' – ralight 2014-10-29 16:58:20

回答

1

我只是很快厭倦了你的代碼,它發佈到messagesight.demos.ibm.com罰款。

example

我做的唯一的事情就是註釋掉的用戶ID,密碼。

#client.username_pw_set(userID, password) 

您是否已經正確安裝了Paho Python Client,同樣也是一個很好的例子。 http://www.eclipse.org/paho/clients/python/

+0

是的,Paho Python客戶端正確安裝...它仍然無法正常工作。我們仍然遇到同樣的問題「無法分配請求地址」。你的client.py文件的內容是什麼?你能找到第260行「listensock.bind((」127.0.0.1「,0))」嗎?謝謝! – 2014-10-29 16:33:19

+2

你能提供 的輸出嗎ifconfig -a' – 2014-10-29 17:25:25

+1

@maria聽起來像其他人提到的那樣,chi網絡配置。 – 2014-10-29 21:16:31