英文:
How to find out that mqtt-paho publish failed due to ACL?
问题
我正在使用paho.mqtt Python库。我在我的MQTT代理上设置了一个ACL列表。然而,我无法看到在我有权限发布的主题和我没有权限发布的主题之间有任何区别。
我以以下方式设置了on_connect
和on_publish
回调函数:
def on_connect(args, client, userdata, flags, rc):
if rc == 0:
print("已连接到MQTT代理!")
msginfo = client.publish(args.topic, args.string, qos=2, retain=True)
time.sleep(1)
print(f"发布 rc {msginfo.rc}")
else:
print(f"连接失败,返回码 {rc}\n")
def on_publish(client, userdata, mid):
print(f"发布成功: 用户数据:{userdata}, 消息ID:{mid}")
我得到了相同的输出:
已连接到MQTT代理!
发布 rc 0
发布成功: 用户数据:None, 消息ID:1
在两种情况下都是这样的。是否有办法获得“由于授权/ACL问题发布失败”的信息?
英文:
I'm using paho.mqtt Python library. I have an ACL list set up on my MQTT broker. However, I can't see any difference between publishing to a topic I have rights to and publishing to a topic I don't have rights to.
I set up on_connect
and on_publish
callbacks in following ways:
def on_connect(args, client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
msginfo = client.publish(args.topic, args.string, qos=2, retain=True)
time.sleep(1)
print(f"Publish rc {msginfo.rc}")
else:
print(f"Failed to connect, return code {rc}\n")
def on_publish(client, userdata, mid):
print(f"On publish: userdata:{userdata}, mid:{mid}")
I get the same output:
Connected to MQTT Broker!
Publish rc 0
On publish: userdata:None, mid:1
in both cases. Is there a way to get the information "Publish failed because of authorization/ACL"?
答案1
得分: 1
不要回答我要翻译的问题。以下是要翻译的内容:
短答案是你不能(在MQTT v3.x)。
稍微长一点的答案是:
MQTT v5引入了响应数据包中的标志,用于发布QOS 1或2的消息(QOS 0不被确认,因此没有方式来表示未经授权)。
如果你的代理支持MQTT v5,那么你可以尝试在连接时向paho客户端传递相关选项(协议=MQTTv5),以查看是否会得到不同的答案。我没有尝试过,所以不能确定它是否会引发一个可以被on_error
回调捕获的错误,还是只是不调用on_published
回调。
英文:
The short answer is you don't (at MQTT v3.x)
Slightly longer answer:
MQTT v5 introduced flags in the response packets for Messages published with QOS 1 or 2 (QOS 0 are not acknowledged so no way to signal not authorised)
https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901124
If your broker supports MQTT v5 then you can try passing the relevant options (protocol=MQTTv5)to the paho client at connect time to see if you get a different answer. I've not tried so can't be sure if it will throw an error that can be caught by the on_error
callback or if it will just not call the on_published
callback.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论