使用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. 转 crs damon can't start 2个例子

    ###sample 1 "node 1 (10.198.127.5): ps -ef|grep ora.crsd root 45613166 47185944 0 10:24:35 pts/ ...

  2. Vue生命周期钩子详解【个人解读】

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. grunt 实例构建(四)

    相关插件的引用: grunt-usemin  对页面的操作 grunt-contrib-cssmin  压缩css load-grunt-tasks 瘦身gruntfile grunt-rev给md5 ...

  4. MySQL修改数据表

    ALTER [IGNORE] table tb_name alter_spec,alter_spec......... alter_specification: ADD [COLUMN] create ...

  5. Ubuntu 下常用的命令 简略记录

    # 动态显示 NVIDIA watch -n 1 nvidia-smi #查看某一目录下文件的总数(不包含子目录) ls -l | wc -l #挂载硬盘或者U盘 mount /dev/sdb1 /m ...

  6. Mybatis 关联查询(三)

    多对多的管理查询结果映射   1. 需求: 查询用户购买的商品信息   2. 分析: (1)用户和商品没有直接关联 (2)用户和订单进行了关联,订单和订单明细进行了关联,订单明细和商品进行了关联,因此 ...

  7. Hibernate查询所有数据的操作

    Hibernate查询所有数据的操作方式有三种. 1.Query (1)使用该方法查询时,不需要编写sql语句,但是需要编写hql(Hibernate Query Language)语句,该语句是Hi ...

  8. weblogic JDBC Connection Pools--转官方文档

    http://docs.oracle.com/cd/E13222_01/wls/docs81/ConsoleHelp/jdbc_connection_pools.html#1106016 JDBC C ...

  9. Redis双机热备方案--转

    http://luyx30.blog.51cto.com/1029851/1350832 参考资料: http://patrick-tang.blogspot.com/2012/06/redis-ke ...

  10. HDU 5694——BD String——————【递归求解】

    BD String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...