使用HiveMQ的Paho Python客户端

时间:2015-11-25 15:39:00

标签: python mqtt paho hivemq

我正在python中开发一个模块,它允许我将我的覆盆子pi连接到我个人电脑上托管的hivemq版本。

它正常连接但是当我添加hivemq的文件auth插件时它似乎无法正常工作

我使用username_pw_set设置我的用户名和密码

这是我的代码目前的样子:

import paho.mqtt.client as mqtt
client = mqtt.Client()

#The callback for when the client recieves a CONNACK response from the server
def on_connect(client, userdata, flags, rc):
  print("connected with the result code "+str(rc))


#Define any topics you would like the pi to
#automatically subscribe to here

#The callback for when this client publishes to the server.
def on_publish(client, userdata, mid):
  print("message published")


#The callback for when a PUBLISH message is recieve from the server.
def on_message(client, userdata, msg):
  print(msg.topic+" "+str(msg.payload))


def on_log(client, userdata, level, buf):
  print(str(level)+" "+str(buf))


#set callbacks
def setup():
  client.on_connect = on_connect
  client.on_message = on_message
  client.on_publish = on_publish
  client.on_log = on_log

#setup connection to broker
def connect(username, password):
  client.username_pw_set(username, password)
  client.connect("10.19.109.152")

def publish(topic, message):
  client.publish(topic, message)

def loop():
  client.loop()

可能与python客户端格式化连接请求的方式有关吗?

编辑:

服务器给我错误信息:

2015-11-26 09:50:53,723 ERROR - Could not get valid results from the webservice
org.apache.http.NoHttpResponseException: The target server failed to respond
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(Default
HttpResponseParser.java:143)

和我的客户端(on_connect函数)输出一个带有代码5的connack消息,该消息用于拒绝连接,因为它未经授权。

1 个答案:

答案 0 :(得分:0)

来自hivemq的错误表明你指向它的http服务器没有响应。

您应该检查这些细节是否正确,以及当您使用curl等测试时它会按预期响应。