import json import paho.mqtt.client as mqtt from tool import push_stream, close_stream, update, exec_sh class MQTT: def on_connect(self, client, userdata, flags, rc): if rc == 0: client.subscribe('lot_mqtt') # Message receiving callback def on_message(self, client, userdata, msg): data = json.loads(msg.payload.decode('utf-8'))["msg"] if data == "push_stream": # 启动推流视频 push_stream() client.publish('success', payload='push_stream success', qos=0) elif data == "close_stream": # 关闭推流视频 close_stream() client.publish('success', payload='close_stream success', qos=0) elif data == "exec": # 执行命令 exec_sh(msg) client.publish('success', payload='exec_sh success', qos=0) elif data == "update": # git更新项目和配置文件 update() client.publish('success', payload='update success', qos=0) else: client.publish('error', payload='No Such Type', qos=0) def __init__(self): self.client = mqtt.Client() self.client.username_pw_set("demo", "123456") # Specify callback function self.client.on_connect = self.on_connect self.client.on_message = self.on_message # Establish a connection self.client.connect('ceshi-mqtt.lihaink.cn', 1883) # Publish a message self.client.loop_forever(retry_first_connection=True) if __name__ == '__main__': mq = MQTT()