【问题标题】:MQTT auth and password not working in python fileMQTT 身份验证和密码在 python 文件中不起作用
【发布时间】:2022-07-07 15:58:10
【问题描述】:

在命令行中使用 mosquitto pub/sub 时,用户和密码工作正常,但是当我尝试运行 .py 文件时,我得到“Received CONNACK (0, 5)”“bad connection Returned”。我错过了什么?

#! c:\python34\python.exe
#!/usr/bin/env python


import paho.mqtt.client as mqtt  
import time

def on_log(client, userdata, level, buf):
        print("log: "+buf)
        client.tls_set()
def on_connect(client, userdata, flags, rc):
    if rc==0:
        print("connected OK")
    else:
        print("Bad connection Returned code=",rc)
def on_disconnect(client, userdata, flags, rc=0):
        print("DisConnected result code "+str(rc))

def on_message(client,userdata,msg):
        topic=msg.topic
        m_decode=str(msg.payload.decode("utf-8","ignore"))
        print("message received",m_decode)
broker="test.mosquitto.org"
client = mqtt.Client("python1")
user="teste"
password="teste"

client.username_pw_set(user,password=password)
client.on_connect=on_connect  
client.on_disconnect=on_disconnect
client.on_log=on_log
client.on_message=on_message
print("Connecting to broker ",broker)

client.connect(broker,1884)      
client.loop_start()  
client.subscribe("house/sensor1")
client.publish("house/sensor1","my first message")
time.sleep(4)
client.loop_stop()    
client.disconnect() 



【问题讨论】:

标签: python passwords mqtt mosquitto


【解决方案1】:

如 cmets 中所述

使用 test.mosquitto.org 代理时,您必须使用 http://test.mosquitto.org 中提到的用户名/密码对。

经过身份验证的侦听器需要用户名/密码:

rw / readwrite : read/write access to the # topic hierarchy
ro / readonly : read only access to the # topic hierarchy
wo / writeonly : write only access to the # topic hierarch

【讨论】:

    猜你喜欢
    • 2018-01-01
    • 1970-01-01
    • 2015-01-15
    • 2012-11-03
    • 1970-01-01
    • 2018-06-17
    • 2020-11-06
    • 1970-01-01
    • 2019-01-29
    相关资源
    最近更新 更多