ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – Mosquitto和last will(遗嘱)的问题

2019-07-16 08:55:07  阅读:701  来源: 互联网

标签:python mqtt mosquitto paho


我正在使用Mosquitto和Paho的Python实现来尝试传达几个程序.当我使用最后一个功能时,我遇到了一些麻烦.我的代码是这样的:

会员键:

import paho.mqtt.client as mqtt
def on_message(client, userdata, msg):
    print 'Received: ' + msg.payload

client = mqtt.Client()
client.on_message = on_message

client.connect('localhost', 1883)
client.subscribe('hello/#')

client.loop_forever()

出版商:

import paho.mqtt.client as mqtt

client = mqtt.Client()

client.will_set('hello/will', 'Last will', 0, False)
client.connect('localhost', 1883)

client.publish('hello/world', 'Regular msg', 0, False)
client.disconnect()

输出:

Received: Last will

我应该只收到常规消息,因为我使用client.disconnect()来关闭连接.如果我评论will_set行,我会收到常规消息.我也尝试在同一主题上发布它们并且它不起作用.

解决方法:

您可以尝试使用单一方法只发布一条消息,如下所示:

import paho.mqtt.publish as publish

publish.single('hello/world', 'Regular msg', 0, False, 'localhost' , 1883, 'publisher', 10, {'topic': 'hello/will', 'payload': 'Will msg', 'qos': 0, 'retain': False})

https://pypi.python.org/pypi/paho-mqtt#single

我猜想问题是你在发布实际完成之前断开连接,这可能就是你看到遗嘱信息的原因.

编辑 –
当我使用mosquitto_sub -v -t’hello /#’运行你的代码时,我看到了正常的消息和正在传递的意愿.

EDIT2 –

这对我来说很好:

import paho.mqtt.client as mqtt

client = mqtt.Client()

client.will_set('hello/will', 'Last will', 0, False)
client.connect('localhost', 1883)

client.publish('hello/world', 'Regular msg', 0, False)
client.loop();
client.disconnect()
client.loop();

标签:python,mqtt,mosquitto,paho
来源: https://codeday.me/bug/20190716/1476520.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有