60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
![]() |
#!/usr/bin/env python3
|
|||
|
# -*- coding: utf-8 -*-
|
|||
|
"""
|
|||
|
快速验证CORS配置
|
|||
|
"""
|
|||
|
|
|||
|
import requests
|
|||
|
import json
|
|||
|
|
|||
|
def test_cors():
|
|||
|
"""快速测试CORS配置"""
|
|||
|
|
|||
|
api_base = "http://127.0.0.1:5000"
|
|||
|
|
|||
|
print("🧪 快速CORS验证")
|
|||
|
print("=" * 40)
|
|||
|
|
|||
|
# 测试基础健康检查
|
|||
|
try:
|
|||
|
print("1. 测试基础连接...")
|
|||
|
response = requests.get(f"{api_base}/api/health", timeout=5)
|
|||
|
print(f" 状态: {response.status_code}")
|
|||
|
print(f" CORS头: {response.headers.get('Access-Control-Allow-Origin', '未设置')}")
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
print(f" ❌ 失败: {e}")
|
|||
|
return
|
|||
|
|
|||
|
# 测试OPTIONS预检
|
|||
|
try:
|
|||
|
print("2. 测试OPTIONS预检...")
|
|||
|
response = requests.options(f"{api_base}/api/training", timeout=5)
|
|||
|
print(f" 状态: {response.status_code}")
|
|||
|
print(f" 允许来源: {response.headers.get('Access-Control-Allow-Origin', '未设置')}")
|
|||
|
print(f" 允许方法: {response.headers.get('Access-Control-Allow-Methods', '未设置')}")
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
print(f" ❌ 失败: {e}")
|
|||
|
return
|
|||
|
|
|||
|
# 测试实际API调用
|
|||
|
try:
|
|||
|
print("3. 测试API调用...")
|
|||
|
headers = {
|
|||
|
'Content-Type': 'application/json',
|
|||
|
'Origin': 'http://localhost:5173'
|
|||
|
}
|
|||
|
response = requests.get(f"{api_base}/api/training", headers=headers, timeout=5)
|
|||
|
print(f" 状态: {response.status_code}")
|
|||
|
print(f" CORS头: {response.headers.get('Access-Control-Allow-Origin', '未设置')}")
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
print(f" ❌ 失败: {e}")
|
|||
|
return
|
|||
|
|
|||
|
print("\n✅ CORS配置验证完成!")
|
|||
|
print("💡 如果所有测试都显示 'Access-Control-Allow-Origin: *',则CORS配置正确")
|
|||
|
|
|||
|
if __name__ == "__main__":
|
|||
|
test_cors()
|