我試圖使用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
我該如何解決?
寫這個感覺有點奇怪,但是你有沒有在你的Pi上支持IPv4? – ralight 2014-10-29 16:51:07
或者您的「lo」界面關閉。嘗試'ifconfig lo down; ifconfig lo up' – ralight 2014-10-29 16:58:20