python 实现MQTT Client

应用Python 实现MQTT Client,主要代码如下:

#coding:utf-8
#!/usr/bin/python3
import json
import os
import binascii
import asn1tools
import sys
import paho.mqtt.client as mqtt
import requests
import logging
from enum import Enum
from queue import Queue

__all__ = ["MQTTClient"]

class MQTTClient:

    def __init__(self,host, port, qos, timeout, log):
        self.host = host
        self.port = port
        self.qos = qos
        self.queue = Queue()
        self.mqtt_client = None
        self.timeout = timeout
        self.log = log

    def on_message(self, client, userdata, msg):
        self.log.debug(‘get a message: %s‘ % msg)
        self.queue.put(msg)

    def subscribe(self, topic):
        self.mqtt_client.subscribe(topic, self.qos)
        self.log.debug(‘subscribe to %s‘ % topic)

    def unsubscribe(self, topic):
        self.mqtt_client.unsubscribe(topic)
        self.log.debug(‘unsubscribe %s‘ % topic)

    def receive_msg(self, timeout=None):
        self.log.debug(‘waiting for message.‘)
        if timeout is None:
            timeout = self.timeout
        return self.queue.get(timeout=timeout)

    def publish(self, topic, blob):
        self.mqtt_client.publish(topic, blob)

    def loop_start(self):
        if self.mqtt_client is None:
            self.mqtt_client = mqtt.Client()
            self.mqtt_client.on_message = self.on_message
            self.mqtt_client.connect(self.host, self.port, self.timeout)
            self.mqtt_client.loop_start()

    def loop_stop(self):
        if self.mqtt_client is not None:
            self.mqtt_client.loop_stop()
            self.mqtt_client.disconnect()
            self.mqtt_client = None

  

原文地址:https://www.cnblogs.com/mftang2018/p/10884565.html

时间: 2024-11-09 04:43:28

python 实现MQTT Client的相关文章

mqtt client python example

This is a simple example showing how to use the [Paho MQTT Python client](https://eclipse.org/paho/clients/python/) to send data to Azure IoT Hub. You need to assemble the rights credentials and configure TLS and the MQTT protocol version appropriate

python mqtt client publish操作

使用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("C

Python实现MQTT接收订阅数据

一.背景 目前MQTT的标准组织官网:http://www.mqtt.org,里面列出了很多支持的软件相关资源. 一个轻量级的MQTT服务器是:http://www.mosquitto.org,可以运行ARM/MIPS的嵌入式linux系统上. 物联网常使用 “消息队列遥测传输(Message Queuing Telemetry Transport, MQTT)” 协议订阅数据,这里用Python实现从MQTT服务器订阅数据. 首先和TCP协议比较 首先TCP是传输层协议,实现了一个双向的通信链

python的scribe client

在网上找了一个python的scribe client使用方法 依赖的模块: pip install facebook-scribe pip install thrift 代码例子: #!/usr/bin/python import sys from scribe import scribe from thrift.transport import TTransport, TSocket from thrift.protocol import TBinaryProtocol category =

mqtt client api: 阻塞API

fusesource版本:mqtt-client-1.11.jar下载地址:https://github.com/fusesource/mqtt-client fusesource提供三种mqtt client api: 阻塞API,基于Futur的API和回调API.其中,回调API是最复杂的也是性能最好的,另外两种均是对回调API的封装. 我们下面就简单介绍一下回调API的使用方法. 1 import org.fusesource.hawtbuf.Buffer; 2 import org.f

php mqtt client

<?php /* phpMQTT */ class phpMQTT { private $socket; /* holds the socket */ private $msgid = 1; /* counter for message id */ public $keepalive = 10; /* default keepalive timmer */ public $timesinceping; /* host unix time, used to detect disconects */

python调用win32com.client的GetObject查找进程信息及服务信息

为何不用wmi呢?因为执行很慢,为啥不用winreg?因为winreg在批量获取及遍历服务方面很不方便,于是采用这方法 该方法同命令行下的wmic执行 获取服务信息 #coding=utf8 from win32com.client import GetObject mywmi = GetObject("winmgmts:") objs = mywmi.InstancesOf("Win32_Service") for obj in objs: print obj.P

Python MQTT 实验

Two client: Pub_1.py import paho.mqtt.client as mqtt import time import sys import random def on_connect(client, userdata, flags, rc): print("connected with result code "+str(rc)) def on_publish(client, userdata, mid): print("Publish au/csi

MQTT学习笔记——树莓派MQTT客户端 使用Mosquitto和paho-python

0 前言 本文说明如何在树莓派上安装Mosquitto.本文通过两个简单的例子说明树莓派中如何使用MQTT协议实现消息订阅,这些例子包括Mosquitto_sub指令实现消息订阅和paho-python扩展库实现GPIO端口的远程控制.本文中使用了两个工具--Mosquitto paho-python,其中Mosquitto是一款实现了 MQTT v3.1 协议的开源消息代理软件,提供轻量级的,支持发布/订阅的的消息推送模式,使设备对设备之间的消息通信简单易用:另外,paho-python是一个