加入执行命令api功能

This commit is contained in:
2026-01-25 10:48:53 +08:00
parent 581515df44
commit 863c0c784c

View File

@@ -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):