使用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. Scrapy——settings配置文件

    # -*- coding: utf-8 -*- # Scrapy settings for tencent project # # For simplicity, this file contains ...

  2. nginx基础学习第二篇:nginx内置变量的使用

    ngx_http_core模块提供的内置变量有很多,常见的有 $uri,用来获取当前请求的uri,不含请求参数. $request_uri,用来获取请求最原始的uri,包含请求参数,且未解码. $re ...

  3. 2019第九届MathorCup数学建模

    题目下载:https://www.lanzous.com/i3taz2j 总共四个问题 问题1 首先附件一中的数据,拿到后肯定感觉棘手.我们的处理方法: 在下面缺失数据的地方我们都认为是问题3中的预测 ...

  4. Tensorflow样例代码分析cifar10

    github地址:https://github.com/tensorflow/models.git 本文分析tutorial/image/cifar10教程项目的cifar10_input.py代码. ...

  5. volatile与synchronized实现原理

    参考文章:https://www.cnblogs.com/charlesblc/p/5994162.html --------------------------------------------- ...

  6. r.js 配置文件 example.build.js 不完整注释

    /* * This is an example build file that demonstrates how to use the build system for * require.js. * ...

  7. 入门Promise的正确姿势

    Promise是异步编程的一种解决方案,从语法上说,Promise是一个对象,从它可以获取异步操作的消息. Promise的基本用法 Promise构造函数接受一个函数作为参数,该函数的两个参数分别是 ...

  8. MySQL之存储引擎(表类型)的选择

    和大部分的数据库不同,MySQL中有一个存储引擎的概念,用户可以根据数据存储的需求来选择不同的存储引擎.本次博客就来介绍一下MySQL中的存储引擎.MySQL版本 5.7.19. 概述 MySQL的存 ...

  9. chmod修改文件的权限/chown修改文件和目录的所有者(转)

    ll指令的显示的信息为(当前目录下只有nameservice1一个目录): drwxr-xr-x 3 hdfs hdfs 4096 4月 14 16:19 nameservice1 上述信息分别表示: ...

  10. Map集合遍历的4种方法

    完全复制https://www.cnblogs.com/blest-future/p/4628871.html import java.util.HashMap; import java.util.I ...