From 863c0c784cd75eaae205f7ffcde3980165d6a741 Mon Sep 17 00:00:00 2001 From: kicer Date: Sun, 25 Jan 2026 10:48:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=89=A7=E8=A1=8C=E5=91=BD?= =?UTF-8?q?=E4=BB=A4api=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rom/app.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/rom/app.py b/src/rom/app.py index 942a1f6..3115500 100644 --- a/src/rom/app.py +++ b/src/rom/app.py @@ -241,6 +241,7 @@ def start(): ), "uptime": str(f"{time.ticks_ms() // 1000} sec"), "memory": str(f"{gc.mem_free() // 1000} KB"), + "uuid": str(machine.unique_id().hex()), "platform": str(sys.platform), "version": str(sys.version), } @@ -300,6 +301,32 @@ def start(): ) await request.write(json.dumps(ack)) + # /exec: 执行命令并返回 + # {"cmd":"import network;R=network.WLAN().config(\"mac\").hex()", "token":"xxx"} + @naw.route("/exec") + async def eval_cmd(request): + ack = {"status": "success"} + try: + if request.method != "POST": + raise Exception("invalid request") + content_length = int(request.headers["Content-Length"]) + post_data = (await request.read(content_length)).decode() + + cmd = json.loads(post_data).get("cmd") + token = json.loads(post_data).get("token") + if cmd and token == machine.unique_id().hex(): + _NS = {} + exec(cmd, _NS) + ack["result"] = str(_NS.get("R")) + except Exception as e: + ack["status"] = "error" + ack["message"] = str(e) + finally: + await request.write( + "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + ) + await request.write(json.dumps(ack)) + # /config: 获取当前配置 @naw.route("/config") async def config_get(request):