#!/usr/bin/env python3 """ 简单的HTTP测试脚本,测试shrinkwrap接口的timeout功能 """ import json import requests import sys import time def test_shrinkwrap_timeout(timeout_seconds=60): """ 测试shrinkwrap接口的超时功能 """ url = "http://localhost:12345/api/creo/shrinkwrap/shell" # 测试数据 test_data = { "software_type": "creo", "project_name": "Timeout Test", "method": "outer_shell", "quality": 5, "chord_height": 0.1, "fill_holes": False, "ignore_small_surfaces": False, "small_surface_percentage": 0.0, "output_file_path": "test_shrinkwrap_timeout.prt", "output_type": "solid_surface", "ignore_quilts": True, "ignore_skeleton": True, "assign_mass_properties": False, "preset": "balanced", "timeout_seconds": timeout_seconds # 使用动态超时 } headers = { "Content-Type": "application/json" } print(f"测试shrinkwrap接口,超时设置: {timeout_seconds}秒") print(f"URL: {url}") print(f"请求数据: {json.dumps(test_data, indent=2, ensure_ascii=False)}") try: start_time = time.time() response = requests.post(url, json=test_data, headers=headers, timeout=timeout_seconds+10) end_time = time.time() print(f"请求耗时: {end_time - start_time:.2f}秒") print(f"HTTP状态码: {response.status_code}") print(f"响应内容: {response.text}") if response.status_code == 200: print("✓ 请求成功") return True elif response.status_code == 504: print("✓ 超时处理正常") return True else: print(f"✗ 请求失败: {response.status_code}") return False except requests.exceptions.Timeout: print("✗ HTTP客户端超时") return False except requests.exceptions.ConnectionError: print("✗ 连接失败,请确保DLL服务正在运行") return False except Exception as e: print(f"✗ 请求异常: {e}") return False def test_different_timeouts(): """ 测试不同的超时设置 """ print("=== 测试不同超时设置 ===\n") # 测试各种超时值 test_cases = [ {"timeout": 15, "desc": "短超时测试"}, {"timeout": 30, "desc": "默认超时测试"}, {"timeout": 120, "desc": "长超时测试"}, {"timeout": 5, "desc": "极短超时测试(应被限制为10秒)"}, {"timeout": 400, "desc": "超长超时测试(应被限制为300秒)"} ] results = [] for case in test_cases: print(f"--- {case['desc']} ---") success = test_shrinkwrap_timeout(case['timeout']) results.append({"case": case['desc'], "success": success}) print() time.sleep(2) # 间隔等待 print("=== 测试结果汇总 ===") for result in results: status = "✓" if result['success'] else "✗" print(f"{status} {result['case']}") if __name__ == "__main__": if len(sys.argv) > 1: try: timeout = int(sys.argv[1]) test_shrinkwrap_timeout(timeout) except ValueError: print("参数错误,请输入有效的超时秒数") sys.exit(1) else: test_different_timeouts()