feat: 新增 bat/sh 版 API 客户端,修复 bat 吞退出码与 CRLF 问题
新增 scripts/api-client.bat 与 api-client.sh:上传图片 -> 轮询 -> 打印识别文本与 抽取字段。自动挑解释器(优先 .venv,退回系统 Python);api_client.py 只用标准库, 所以一台只装 Python、不装本项目依赖的机器也能当客户端用。 修掉两个 bat 上的真 bug(都是实测出来的,不是看代码看出来的): 1. 退出码被吞。`if exist ... ( ... exit /b %errorlevel% )` 里的 %errorlevel% 是**解析时**展开的,取到的是启动程序之前的值,于是失败也返回 0。 start.bat 同样中招(启动失败会被吞成成功)。两处都改成 goto 结构。 2. .bat 必须 CRLF。同一份内容,CRLF 正常跑完;LF 会让 cmd 解析挂住—— 无任何输出、也不退出,看起来像服务卡死。download-models.bat 原本也是 LF, 一并修正;新增 .gitattributes 钉死 *.bat=eol:crlf、*.sh=eol:lf。 另:参数校验挪到探活之前,参数写错时不会因为连不上服务报出误导性错误; stdout 加 flush,重定向到文件时输出顺序不再错乱。 测试:新增两条回归用例(bat 必须 CRLF;Windows 上 bat 能被 cmd 跑通,带超时 以免坏掉时把测试卡死)。99 passed。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -114,7 +114,7 @@ def print_result(data: dict, show_json: bool) -> None:
|
||||
|
||||
|
||||
def process_one(host: str, image_path: Path, args: argparse.Namespace) -> bool:
|
||||
print(f"[{image_path.name}]")
|
||||
print(f"[{image_path.name}]", flush=True)
|
||||
if not image_path.is_file():
|
||||
print(f" 文件不存在:{image_path}", file=sys.stderr)
|
||||
return False
|
||||
@@ -170,7 +170,9 @@ def main() -> int:
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen(f"{host}/healthz", timeout=10) as response:
|
||||
print(f"服务 {host} 正常:{json.loads(response.read().decode('utf-8'))}")
|
||||
# flush:stdout 重定向到文件/管道时是块缓冲的,不刷的话这条会跑到
|
||||
# 后面 stderr 的错误信息之后,看起来像是顺序错乱。
|
||||
print(f"服务 {host} 正常:{json.loads(response.read().decode('utf-8'))}", flush=True)
|
||||
except urllib.error.HTTPError as exc:
|
||||
print(f"服务 {host} 响应异常:HTTP {exc.code}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user