使用Python库paho.mqtt.client 模拟mqtt client 连接broker,publish topic。

#-*-coding:utf-8-*-
import paho.mqtt.client as mqtt class mqttHandle(object): def __init__(self,mqtt_info):
self.mqtt_info=mqtt_info def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("chat") def on_message(client, userdata, msg):
print("topic:" + msg.topic + " payload:" + str(msg.payload)) def publish(self):
client = mqtt.Client()
client.on_connect = mqttHandle.on_connect
client.on_message = mqttHandle.on_message
client.username_pw_set(self.mqtt_info['username'], self.mqtt_info['password'])
client.connect(self.mqtt_info['host'], self.mqtt_info['port'], 60)
client.publish(self.mqtt_info['topic'], str(self.mqtt_info['payload']))
#client.loop_forever()
client.disconnect()
print('publish topic over') if __name__=="__main__":
mqtt_info={
'username':'username',
'password':'password',
'host':'10.10.10.10',
'port':1833,
'topic':'test',
'payload':'hello world',
}
mqttc=mqttHandle(mqtt_info)
mqttc.publish()

python mqtt client publish操作的更多相关文章

  1. mqtt client python example

    This is a simple example showing how to use the [Paho MQTT Python client](https://eclipse.org/paho/c ...

  2. mqtt异步publish方法

    Python基于mqtt异步编程主要用到asyncio及第三方库hbmqtt,这里主要介绍mqtt的异步发布及遇到的一些问题. hbmqtt安装很简单,pip hbmqtt install. mqtt ...

  3. Python MQTT 最简单例程搭建

    MQTT 不是普通的 client server 模型,他还加了一个 代理者. 根据剑锋的提示,先下载了 paho-mqtt 模块, ubuntu 14.04 上下载方法如下: sudo apt-ge ...

  4. Python MQTT客户端实现

    1.安装paho-mqtt 使用Python Package Index (PyPi) pip install paho-mqtt 使用virtualenv virtualenv paho-mqtt ...

  5. go ---MQTT client

    Paho GO Client   语言 GO 协议 EPL AND EDL 官网地址 http://www.eclipse.org/paho/ API类型 Asynchronous  描述 Paho ...

  6. python mqtt 客户端实现

    安装paho-mqtt pip install paho-mqtt -i http://pypi.douban.com/simple --trusted-host pypi.douban.com py ...

  7. python mqtt通信(windows)

      一.消息队列服务器 这里我用到activemq,可到官网下载 http://activemq.apache.org/ 1. 若遇到点击apache-activemq-5.16.2\bin\acti ...

  8. Python 使用Python远程连接并操作InfluxDB数据库

    使用Python远程连接并操作InfluxDB数据库 by:授客 QQ:1033553122 实践环境 Python 3.4.0 CentOS 6 64位(内核版本2.6.32-642.el6.x86 ...

  9. M2Mqtt is a MQTT client available for all .Net platform

    Introduction M2Mqtt is a MQTT client available for all .Net platform (.Net Framework, .Net Compact F ...

随机推荐

  1. my.工坊_ZZ

    1.查了下,可以将 考古升上去,但是 还是使用 2级的考古技能,这样比较赚,高级的反而不赚.但是看到有人说 考古升到 3/4 不能再用 洛阳铲 了. 于是有了两种情况,我暂定的做法:先将 考古升到2级 ...

  2. 文献综述十五:基于b/s中小型超市进销存管理系统的研究与设计

    一.基本信息 标题:基于b/s中小型超市进销存管理系统的研究与设计 时间:2015 出版源:湘西财经大学 文件分类:对超市管理系统的研究 二.研究背景 在竞争日益激烈的行业中,尽可能降低运营成本,逐步 ...

  3. Vue axios 上传图片

    上传图片接口 // 上传图片 export const uploadBanner = formData => { return axios.request({ url: 'manage/slid ...

  4. 自学springboot

    参考资料 https://www.renren.io/guide/

  5. (转)Linux服务器SNMP常用OID

    原文:https://www.haiyun.me/archives/linux-snmp-oid.html 收集整理一些Linux下snmp常用的OID,用做服务器监控很不错.服务器负载: 1 2 3 ...

  6. ASP.NET MVC 域名泛解析设置

    最近有个需求要做一个动态二级域名的网站,我们可以通过这样的方式去访问我们的网站 http://用户名.blog.com.而这里的用户名是根据程序的需要动态生成的.这里就会涉及到DNS服务器,要做相应的 ...

  7. JVM的内存分配和回收策略

    对象的Class加载 虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已被加载.解析和初始化过.如果没有,那必须先执行相应 ...

  8. linux多线程同步

    1. 互斥量是线程同步的一种机制,用来保护多线程的共享资源.同一时刻,只允许一个线程对临界区进行访问.互斥量的工作流程:创建一个互斥量,把这个互斥量的加锁调用放在临界区的开始位置,解锁调用放到临界区的 ...

  9. shiro入门与认证原理

    一.shiro介绍 1.什么是shiro  shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. 2.shiro的优点  (1)shiro将安全认证相关的功能抽取出 ...

  10. Ubuntu apache

    Linux系统为Ubuntu 1. 启动apache服务 # /etc/init.d/apache2 start 2. 重启apache服务 # /etc/init.d/apache2 restart ...