"""验证打包后的 EXE: 等端口 -> 登录 -> 访问关键页面 -> Excel 导出 用法: 先手动启动 dist/MaterialWMS.exe, 再 python tools/check_exe.py """ import sys, time, urllib.request, urllib.parse BASE = "http://127.0.0.1:8080" def wait_port(t=40): for i in range(t): try: urllib.request.urlopen(BASE + "/login", timeout=2) return True except Exception: time.sleep(1) return False def get(path, cookie=None): req = urllib.request.Request(BASE + path) if cookie: req.add_header("Cookie", cookie) return urllib.request.urlopen(req, timeout=5) def post(path, data, cookie=None): req = urllib.request.Request(BASE + path, data=urllib.parse.urlencode(data).encode(), method="POST") if cookie: req.add_header("Cookie", cookie) return urllib.request.urlopen(req, timeout=5) def main(): if not wait_port(): print("[FAIL] 端口 8080 未就绪 (EXE 可能未启动或解压慢)") sys.exit(1) print("[OK] EXE 服务已就绪") # 登录页 r = get("/login") assert "登录" in r.read().decode("utf-8", "ignore") print("[OK] 登录页(冻结模板加载正常)") # 登录 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor()) req = urllib.request.Request(BASE + "/login", data=urllib.parse.urlencode({"username": "admin", "password": "admin123"}).encode(), method="POST") resp = opener.open(req) body = resp.read().decode("utf-8", "ignore") assert "库存看板" in body, "登录后未进入看板" print("[OK] 管理员登录成功, 看板渲染正常") # 关键页面(冻结模板 + 路由) for p in ["/boms", "/orders", "/report", "/logs", "/users", "/import/materials", "/template/bom"]: rr = opener.open(BASE + p) assert rr.status == 200 print("[OK] BOM/订单/报表/日志/用户/导入/模板 页面均可访问") # 导出 Excel rr = opener.open(BASE + "/export/inventory") data = rr.read() assert rr.status == 200 and len(data) > 1000 print(f"[OK] 库存 Excel 导出成功 ({len(data)} 字节)") print("\nEXE 端到端验证通过 ✅") if __name__ == "__main__": main()