import paho.mqtt.client as mqtt def on_connect(client, userdata, flags, rc,properties): print("Connected with result code " + str(rc)) client.subscribe("test/image") def on_message(client, userdata, msg): with open("received_image.jpg", "wb") as image_file: image_file.write(msg.payload) print("Image received and saved") client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, ) client.on_connect = on_connect client.on_message = on_message client.connect("121.40.129.71", 48835, 60) # 连接到 MQTT 代理 client.loop_forever()