python实现mqtt消息转Tcp消息

config.json

python 复制代码
{
    "mqtt_host": "broker.emqx.io",
    "mqtt_port": 1883,
    "mqtt_username": "your_username",
    "mqtt_password": "your_password",
    "publish_topic": "test/topic/publish",
    "subscribe_topic": "test/topic/subscribe",
    "tcp_port": 54321
}
复制代码
mqtt_tcp_bridge.py
python 复制代码
import json
import logging
import socket
import socketserver
import threading
from paho.mqtt import client as mqtt_client

# 配置日志格式
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

# 全局变量和锁,用于管理TCP客户端连接
tcp_clients = []
tcp_clients_lock = threading.Lock()

def load_config(config_file='config.json'):
    """加载配置文件"""
    try:
        with open(config_file, 'r') as f:
            config = json.load(f)
        logging.info("配置文件加载成功")
        return config
    except Exception as e:
        logging.error(f"加载配置文件失败: {e}")
        raise

def on_mqtt_connect(client, userdata, flags, rc):
    """MQTT连接回调"""
    if rc == 0:
        logging.info("MQTT连接成功")
        # 订阅配置中的主题
        client.subscribe(userdata['subscribe_topic'])
    else:
        logging.error(f"MQTT连接失败,错误码:{rc}")

def on_mqtt_message(client, userdata, msg):
    """MQTT消息接收回调"""
    try:
        data = msg.payload.decode()
        logging.info(f"收到订阅消息: {data} (主题: {msg.topic})")
        # 将消息转发给所有TCP客户端
        with tcp_clients_lock:
            for conn in tcp_clients.copy():  # 使用copy避免迭代时修改
                try:
                    conn.sendall(data.encode() + b'\n')  # 添加换行符便于客户端读取
                except Exception as e:
                    logging.error(f"发送数据到TCP客户端失败: {e}")
                    tcp_clients.remove(conn)
    except Exception as e:
        logging.error(f"处理MQTT消息失败: {e}")

class TCPRequestHandler(socketserver.BaseRequestHandler):
    """TCP请求处理类"""
    def handle(self):
        # 客户端连接时添加到列表
        with tcp_clients_lock:
            tcp_clients.append(self.request)
        logging.info(f"新的客户端连接: {self.client_address}")
        
        # 获取MQTT客户端实例用于发布消息
        mqtt_client = self.server.mqtt_client
        publish_topic = self.server.publish_topic
        
        while True:
            try:
                data = self.request.recv(1024)
                if not data:
                    break  # 客户端断开连接
                message = data.decode().strip()
                logging.info(f"收到客户端数据: {message}")
                # 发布到MQTT主题
                mqtt_client.publish(publish_topic, message)
            except (ConnectionResetError, BrokenPipeError):
                break
            except Exception as e:
                logging.error(f"处理客户端数据错误: {e}")
                break
        
        # 客户端断开时移除
        with tcp_clients_lock:
            if self.request in tcp_clients:
                tcp_clients.remove(self.request)
        logging.info(f"客户端断开: {self.client_address}")

class ThreadingTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
    """支持多线程的TCP服务器"""
    daemon_threads = True  # 主程序退出时自动结束线程
    allow_reuse_address = True  # 允许地址复用

def run_mqtt_client(config, server):
    """运行MQTT客户端"""
    client = mqtt_client.Client()
    client.user_data_set(config)  # 传递配置数据
    client.on_connect = on_mqtt_connect
    client.on_message = on_mqtt_message
    
    # 设置用户名密码
    if config.get('mqtt_username'):
        client.username_pw_set(config['mqtt_username'], config['mqtt_password'])
    
    try:
        client.connect(config['mqtt_host'], config['mqtt_port'])
        server.mqtt_client = client  # 将MQTT客户端实例绑定到服务器
        client.loop_start()
        return client
    except Exception as e:
        logging.error(f"MQTT连接错误: {e}")
        raise

def main():
    # 加载配置
    config = load_config()
    server_config = {
        'host': '0.0.0.0',
        'port': config['tcp_port'],
        'publish_topic': config['publish_topic']
    }
    
    # 启动TCP服务器
    with ThreadingTCPServer(
        (server_config['host'], server_config['port']), TCPRequestHandler
    ) as server:
        server.publish_topic = server_config['publish_topic']
        logging.info(f"TCP服务器启动,监听 {server_config['host']}:{server_config['port']}")
        
        # 启动MQTT客户端并绑定到服务器实例
        mqtt_client = run_mqtt_client(config, server)
        
        try:
            server.serve_forever()
        except KeyboardInterrupt:
            logging.info("收到中断信号,关闭服务器...")
            server.shutdown()
            mqtt_client.loop_stop()
            logging.info("服务器已关闭")

if __name__ == '__main__':
    main()
python 复制代码
pip install paho-mqtt

python mqtt_tcp_bridge.py
相关推荐
有代理ip1 小时前
网络隐私防护指南:代理服务与换 IP 工具的科学结合
网络·tcp/ip·web安全
lulu12165440782 小时前
Claude Code Harness架构技术深度解析:生产级AI Agent工程化实践
java·人工智能·python·ai编程
不是书本的小明2 小时前
阿里云专有云网络架构
网络·阿里云·架构
mounter6252 小时前
【内核前沿】从 veth 到 netkit:深度解析 TCP devmem 穿透容器屏障的“队列租赁”黑科技
网络·ebpf·linux kernel·devmem tcp·netkit·队列租赁
爱学习的小囧3 小时前
vSphere Supervisor 服务配置指南:自签名容器注册表使用教程
服务器·网络·esxi·虚拟化·vcf
pjwonline13 小时前
反向仲裁:去中心化知识网络中的社会性共识引擎
网络·人工智能·去中心化·区块链·智能合约
空中海4 小时前
5.1 HTTP 与网络请求
网络·网络协议·flutter·http
7年前端辞职转AI4 小时前
Python 文件操作
python·编程语言
龙文浩_4 小时前
AI梯度下降与PyTorch张量操作技术指南
人工智能·pytorch·python·深度学习·神经网络·机器学习·自然语言处理
呱牛do it4 小时前
企业级绩效考核系统设计与实现:基于FastAPI + Vue3的全栈解决方案
python·fastapi