Files
cad-agent/test_autoagent.py
T

43 lines
1.2 KiB
Python
Raw Normal View History

2026-09-28 22:13:47 +08:00
import socket
import json
2026-10-06 23:10:38 +08:00
import base64
2026-09-28 22:13:47 +08:00
2026-10-06 23:10:38 +08:00
def test_screenshot():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 8080))
req = {
2026-10-02 20:39:34 +08:00
"jsonrpc": "2.0",
2026-10-06 23:10:38 +08:00
"id": 5,
2026-10-02 20:39:34 +08:00
"method": "tools/call",
"params": {
2026-10-06 23:10:38 +08:00
"name": "get_viewport_screenshot",
"arguments": {}
2026-10-02 20:39:34 +08:00
}
}
2026-10-06 23:10:38 +08:00
client.sendall(json.dumps(req).encode('utf-8'))
2026-10-02 23:43:18 +08:00
2026-10-06 23:10:38 +08:00
# 图片 base64 报文可能很大,需要循环接收直到拿完
response_data = b""
while True:
chunk = client.recv(8192)
response_data += chunk
if len(chunk) < 8192:
break
client.close()
2026-10-02 23:43:18 +08:00
2026-10-06 23:10:38 +08:00
resp_json = json.loads(response_data.decode('utf-8'))
2026-10-02 23:43:18 +08:00
2026-10-06 23:10:38 +08:00
# 解析并保存图片
content_list = resp_json["result"]["content"]
for item in content_list:
if item["type"] == "image":
base64_data = item["data"]
image_bytes = base64.b64decode(base64_data)
with open("test_screenshot.png", "wb") as f:
f.write(image_bytes)
print("截图已保存为 test_screenshot.png,请在当前目录查看!")
if __name__ == "__main__":
test_screenshot()