Paho Python Client with HiveMQ

2019-06-13 17:09发布

问题:

i am developing a module in python that will allow me to connect my raspberry pi to a version of hivemq hosted on my pc.

it connects normally but when i add hivemq's file auth plugin it doesnt seem to work

im using the username_pw_set to set my username and password

here is what my code currently looks like:

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

could it be something to do with the way the python client formats the connect request?

Edit:

Server gives me error message:

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)

and my client(on_connect function) outputs a connack message with code 5 which is for refused connection as its not authorised.

回答1:

The error from hivemq states that the http server you have pointed it at to do the authentication has not responded.

You should check that those details are correct and that it responds as expected when you use something like curl to test it.