首页 文章

在覆盆子pi中使用mqtt paho for Python

提问于
浏览
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()

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

Traceback(最近一次调用最后一次):文件“ejemplo.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中listensock.bind((“localhost”,0))文件“/usr/lib/python2.7/socket.py”,第224行,在meth return getattr(self._sock,name)(* args)socket.error: [Errno 99]无法分配请求的地址

我该如何解决?

1 回答

  • 1

    我只是很快就厌倦了你的代码而且发布到 messagesight.demos.ibm.com 很好 .

    example

    我唯一做的就是注释掉userID,密码 .

    #client.username_pw_set(userID, password)
    

    你是否正确安装了Paho Python客户端,也是一个很好的例子 . http://www.eclipse.org/paho/clients/python/

相关问题