python使用mqtt

一、安装mqtt服务器

安装对应的软件:https://www.emqx.io/zh/downloads

推荐使用docker安装

默认账号和密码:admin、public

设置用户认证信息(注意:认证信息和登录信息时不一样的

 

二、编写代码

消息发布程序

import time
import json
import psutil
import random
from paho.mqtt import client as mqtt_client

broker = '127.0.0.1'  # mqtt代理服务器地址
port = 1883
keepalive = 60  # 与代理通信之间允许的最长时间段(以秒为单位)
topic = "/python/mqtt"  # 消息主题

# 设置客户端ID和认证信息
client_id = f'python-mqtt-pub-{random.randint(0, 1000)}'  # 客户端id不能重复
username = "admin"
password = "123456"


def to_M(n):
    '''将B转换为M'''
    u = 1024 * 1024
    m = round(n / u, 2)
    return m


def get_info():
    '''获取系统硬件信息:cpu利用率,cpu个数,系统负载,内存信息等'''
    cpu_percent = psutil.cpu_percent(interval=1)
    cpu_count = psutil.cpu_count()
    sys_loadavg = [round(x / psutil.cpu_count() * 100, 2) for x in psutil.getloadavg()]
    mem = psutil.virtual_memory()
    mem_total, men_free = to_M(mem.total), to_M(mem.free)
    mem_percent = mem.percent
    info = {
        'cpu_percent': cpu_percent,
        'cpu_count': cpu_count,
        'sys_loadavg': sys_loadavg,
        'mem_total': mem_total,
        'mem_percent': mem_percent,
        'men_free': men_free
    }
    # mqtt只能传输字符串数据
    return json.dumps(info)


def connect_mqtt():
    '''连接mqtt代理服务器'''

    def on_connect(client, userdata, flags, rc):
        '''连接回调函数'''
        # 响应状态码为0表示连接成功
        if rc == 0:
            print("Connected to MQTT OK!")
        else:
            print("Failed to connect, return code %d\n", rc)

    # 连接mqtt代理服务器,并获取连接引用
    client = mqtt_client.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(broker, port, keepalive)
    return client


def publish(client):
    '''发布消息'''
    while True:
        '''每隔4秒发布一次服务器信息'''
        time.sleep(4)
        msg = get_info()
        result = client.publish(topic, msg)
        status = result[0]
        if status == 0:
            print(f"Send `{msg}` to topic `{topic}`")
        else:
            print(f"Failed to send message to topic {topic}")


def run():
    '''运行发布者'''
    client = connect_mqtt()
    # 运行一个线程来自动调用loop()处理网络事件, 非阻塞
    client.loop_start()
    publish(client)


if __name__ == '__main__':
    run()

 

消息订阅程序

import random
from paho.mqtt import client as mqtt_client

broker = '127.0.0.1'  # mqtt代理服务器地址
port = 1883
keepalive = 60  # 与代理通信之间允许的最长时间段(以秒为单位)
topic = "/python/mqtt"  # 消息主题

# 设置客户端ID和认证信息
client_id = f'python-mqtt-sub-{random.randint(0, 1000)}'  # 客户端id不能重复
username = "admin"
password = "123456"


def connect_mqtt():
    '''连接mqtt代理服务器'''

    def on_connect(client, userdata, flags, rc):
        '''连接回调函数'''
        # 响应状态码为0表示连接成功
        if rc == 0:
            print("Connected to MQTT OK!")
        else:
            print("Failed to connect, return code %d\n", rc)

    client = mqtt_client.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(broker, port, keepalive)
    return client


def subscribe(client: mqtt_client):
    '''订阅主题并接收消息'''

    def on_message(client, userdata, msg):
        '''订阅消息回调函数'''
        print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")

    # 订阅指定消息主题
    client.subscribe(topic)
    client.on_message = on_message


def run():
    # 运行订阅者
    client = connect_mqtt()
    subscribe(client)
    #  运行一个线程来自动调用loop()处理网络事件, 阻塞模式
    client.loop_forever()


if __name__ == '__main__':
    run()

 

posted @ 2023-08-03 11:20  程序员小艺  阅读(337)  评论(0编辑  收藏  举报