在树莓派中使用mqtt paho for Python

时间:2014-10-28 17:12:34

标签: python-2.7 mqtt paho

我正在尝试使用raspthon中的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()

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

  

回溯(最近一次调用最后一次):文件“ejemplo.py”,第27行,in          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 个答案:

答案 0 :(得分:1)

我很快就厌倦了你的代码,它发布到messagesight.demos.ibm.com很好。

example

我唯一做的就是注释掉用户ID,密码。

#client.username_pw_set(userID, password)

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

相关问题