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