0

我正在用 python 开发一个模块,它允许我将我的树莓派连接到我的电脑上托管的 hivemq 版本。

它连接正常,但是当我添加 hivemq 的文件身份验证插件时,它似乎不起作用

我使用 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 消息,该消息用于拒绝连接,因为它未经授权。

4

1 回答 1

0

hivemq 的错误表明您指向它进行身份验证的 http 服务器没有响应。

您应该检查这些详细信息是否正确,并且当您使用 curl 之类的东西对其进行测试时,它是否按预期响应。

于 2015-11-26T10:27:50.857 回答