使用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. 转 oracle ASM中ASM_POWER_LIMIT参数

    https://daizj.iteye.com/blog/1753434 ASM_POWER_LIMIT 该初始化参数用于指定ASM例程平衡磁盘所用的最大权值,其数值范围为0~11,默认值为1.该初始 ...

  2. 一篇在一个Excel表中创建多个sheet的代码

    package projectUtil; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermod ...

  3. oracle--dump->buffer cache (dump 深入实践一)

    1,dump 取值 ALTER SESSION SET EVENTS 'immediate trace name buffers level n'; 只转储buffer header. 在level ...

  4. HBase数据快速导入之ImportTsv&Bulkload

    导入数据最快的方式,可以略过WAL直接生产底层HFile文件 (环境:centos6.5.Hadoop2.6.0.HBase0.98.9) 1.SHELL方式 1.1 ImportTsv直接导入 命令 ...

  5. 实现JS继承的几种方法

    总的来说,JS的继承大体上分为两种:借用构造函数方式和原型方式 首先,我们来看看借用构造函数方式的几种做法: //方式一function Person(name, sex){ this.name = ...

  6. html中行级元素的居中显示。

    垂直居中.以label标签为例. <style> #label1{ vertical-align:middle; line-height:40px;<*父元素的height*> ...

  7. html中块元素的居中。及兼容性

    块在块中垂直居中(父元素postion:relative;   子元素position:absolute; top:50%; margin-top:负二分之一高度) 块在块中水平居中 (子元素marg ...

  8. Lucene学习之四:Lucene的索引文件格式(3)

    本文转载自:http://www.cnblogs.com/forfuture1978/archive/2010/02/02/1661436.html ,略有删改和备注. 四.具体格式 4.2. 反向信 ...

  9. ASP.NET MVC4 新手入门教程之六 ---6.编辑视图与编辑方法

    在本节中,您会为电影控制器检查生成的操作方法和视图.然后,您将添加一个自定义的搜索页面. 运行该应用程序,然后浏览到Movies控制器通过将/Movies追加到您的浏览器的地址栏中的 URL.将鼠标指 ...

  10. JRebel - 给IDE安装JRebel插件

    JRebel对于很多人来说已经并不陌生了,一搜一大把. 用过JRebel后发现,这对于Java开发简直不可缺少. 尽管其价格有点春节国庆期间的各种交通费用——打劫! 即使如此也出现了有"分享 ...