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:
zimoyin
2026-10-14 12:15:39 +08:00
parent 0a299dec18
commit ec6b9c7ae3
7 changed files with 162 additions and 32 deletions
+21 -6
View File
@@ -933,17 +933,32 @@ extract_include_empty: true
### 命令行客户端
不想记接口细节就用 `scripts/api_client.py`,它只用标准库,不依赖 requests,可直接拷到别的机器上跑:
不想记接口细节就用现成的客户端,它只用标准库(不依赖 requests),可直接拷到别的机器上跑:
```bat
rem Windows(也可以直接把图片拖到 bat 上)
scripts\api-client.bat 图片.png
scripts\api-client.bat 图片.png --min-confidence 0.6
scripts\api-client.bat --health
```
```bash
# 服务默认在 http://127.0.0.1:8000,可用 --host 指定别的地址
# Linux / 源码环境,等价
./scripts/api-client.sh 图片.png
python scripts/api_client.py 图片.png
python scripts/api_client.py 图片1.png 图片2.png # 多张依次处理
python scripts/api_client.py 图片.png --min-confidence 0.6 # 只要置信度 >= 0.6 的片段
python scripts/api_client.py 图片.png --json # 打印原始 JSON 响应
python scripts/api_client.py --health # 只探活
```
常用参数(服务默认在 `http://127.0.0.1:8000`):
| 参数 | 说明 |
| --- | --- |
| `图片1 图片2 …` | 要识别的图片,可给多个,依次处理 |
| `--host URL` | 换服务地址,例如 `--host http://192.168.1.10:8000` |
| `--min-confidence 0.6` | 只返回置信度 ≥ 0.6 的片段(服务端过滤,实测 0.999 时 34 段 → 2 段) |
| `--json` | 打印完整 JSON 响应 |
| `--quiet` | 不打印轮询进度 |
| `--health` | 只探活 |
输出示例:
```text