Files
aiagent/backend/test_cors.py
2026-01-23 09:49:45 +08:00

54 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
测试CORS配置
"""
import requests
# 测试CORS配置
def test_cors():
base_url = "http://101.43.95.130:8037"
# 测试OPTIONS请求CORS预检
print("测试CORS预检请求...")
headers = {
"Origin": "http://101.43.95.130:8038",
"Access-Control-Request-Method": "GET",
"Access-Control-Request-Headers": "Content-Type"
}
try:
response = requests.options(f"{base_url}/api/v1/tools/builtin", headers=headers)
print(f"OPTIONS请求状态码: {response.status_code}")
print(f"CORS头信息:")
for key, value in response.headers.items():
if key.lower().startswith('access-control'):
print(f" {key}: {value}")
except Exception as e:
print(f"OPTIONS请求失败: {e}")
# 测试GET请求
print("\n测试GET请求...")
headers = {
"Origin": "http://101.43.95.130:8038"
}
try:
response = requests.get(f"{base_url}/api/v1/tools/builtin", headers=headers)
print(f"GET请求状态码: {response.status_code}")
print(f"CORS头信息:")
for key, value in response.headers.items():
if key.lower().startswith('access-control'):
print(f" {key}: {value}")
if response.status_code == 200:
print(f"\n✅ 请求成功!")
data = response.json()
print(f"返回工具数量: {len(data)}")
else:
print(f"\n❌ 请求失败: {response.text}")
except Exception as e:
print(f"GET请求失败: {e}")
if __name__ == "__main__":
test_cors()