paho.mqtt.python
paho.mqtt.python copied to clipboard
Publish returns success if wrong credentials are used and message is not delivered.
Context
The publish function returns MQTT_ERR_SUCCESS when wrong combination of login/password is used.
How to reproduce
Run following code with correct ip address of the server and login/password with correct combination in the first call of username_pw_set and wrong login or password in the second call of username_pw_set.
import paho.mqtt.client as paho broker="192.168.1.80" port=1883 mosquitto = paho.Client("client1") mosquitto.username_pw_set("login name","correct password") mosquitto.connect(broker,port)
res1= mosquitto.publish("topic1","message1") print("Return with correct credentials: " + str(res1.rc)) mosquitto.disconnect mosquitto.username_pw_set("login name","wrong password") mosquitto.connect(broker,port)
res2 = mosquitto.publish("topic2","message2") print("Return with wrong credentials: " + str(res2.rc)) mosquitto.disconnect
Additional note
MQTT_ERR_SUCCESS = 0 returned in rc in both cases.
What's wrong here? Do I understand it correctly as a bug? Thanks a lot!
I am seeing the same issue. Any updates here would be greatly appreciated.
I believe that this is efficiently a duplicate of #454. As mentioned in my recent comment on that issue connect
returns when the network connection is up and the CONNECT
packet has been sent to the broker; this is before the CONNACK
is received/processed (and the CONNACK
will never be processed in the code above because there is no network loop).
The CONNECT
packet includes the username/password so the connect
function is returning before the broker has checked these.
If you want to connect, send a message, and then disconnect I'd suggest using publish.Single/publish.Multiple. These functions wait for the on_connect
callback to be called (meaning the CONNACK
has been processed) before publishing (and run a network loop for you). Alternatively run a loop and publish from the on_connect
callback.
I appreciate the response @MattBrittan. This is a helpful explanation!
I'm going to close this because I believe the updated docs now offer a better explanation (and all of the examples include a network loop).