2

我正在尝试使用 raspbian 中的 python 2.7 连接到消息代理,如下所示:

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()

当我运行脚本时,我收到以下错误:

回溯(最后一次调用):文件“ejemlo.py”,第 27 行,在 client=paho.Client() 文件“/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py ”,第 410 行,在init self._sockpairR,self._sockpairW = _socketpair_compat() 文件“/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”,第 255 行,在 _socketpair_compat 监听.bind(("localhost", 0)) 文件“/usr/lib/python2.7/socket.py”,第 224 行,方法返回 getattr(self._sock,name)(*args) socket.error: [ Errno 99] 无法分配请求的地址

我该如何解决?

4

1 回答 1

1

I just quickly tired your code and it is publishing to messagesight.demos.ibm.com fine.

example

The only thing I did was comment out the userID, password.

#client.username_pw_set(userID, password)

Have you installed Paho Python Client correctly, also good example there as well. http://www.eclipse.org/paho/clients/python/

于 2014-10-28T23:04:49.030 回答