import paho.mqtt.client as mqtt def publish_image(mqtt_client, topic, image_path): with open(image_path, "rb") as image_file: image_data = image_file.read() mqtt_client.publish(topic, image_data) client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2,) client.connect("121.40.129.71", 48835, 60) # 连接到 MQTT 代理 client.loop_start() img_path = input("Enter the path of the image: ") publish_image(client, "test/image", img_path) client.loop_stop()