quick_cors_test.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. """
  3. 快速CORS测试脚本
  4. """
  5. import requests
  6. from datetime import datetime
  7. def test_cors():
  8. """测试CORS配置"""
  9. base_url = "http://company.citupro.com:5500"
  10. endpoint = "/api/data_parse/get-calendar-info"
  11. print("=== CORS配置测试 ===")
  12. print(f"测试时间: {datetime.now()}")
  13. print(f"目标服务器: {base_url}")
  14. print(f"测试端点: {endpoint}")
  15. print("=" * 50)
  16. # 测试1: OPTIONS预检请求
  17. print("1. 测试OPTIONS预检请求...")
  18. try:
  19. headers = {
  20. 'Origin': 'http://localhost:5173',
  21. 'Access-Control-Request-Method': 'GET',
  22. 'Access-Control-Request-Headers': 'Content-Type'
  23. }
  24. response = requests.options(f"{base_url}{endpoint}", headers=headers)
  25. print(f" 状态码: {response.status_code}")
  26. # 检查CORS头部
  27. cors_headers = []
  28. for key, value in response.headers.items():
  29. if key.lower().startswith('access-control'):
  30. cors_headers.append(f"{key}: {value}")
  31. if cors_headers:
  32. print(" CORS头部:")
  33. for header in cors_headers:
  34. print(f" {header}")
  35. else:
  36. print(" ❌ 未找到CORS头部")
  37. except Exception as e:
  38. print(f" ❌ 预检请求失败: {e}")
  39. print()
  40. # 测试2: 实际GET请求
  41. print("2. 测试实际GET请求...")
  42. try:
  43. today = datetime.now().strftime("%Y-%m-%d")
  44. headers = {'Origin': 'http://localhost:5173'}
  45. response = requests.get(f"{base_url}{endpoint}?date={today}", headers=headers)
  46. print(f" 状态码: {response.status_code}")
  47. if response.status_code == 200:
  48. print(" ✅ 请求成功")
  49. # 检查响应中的CORS头部
  50. cors_headers = []
  51. for key, value in response.headers.items():
  52. if key.lower().startswith('access-control'):
  53. cors_headers.append(f"{key}: {value}")
  54. if cors_headers:
  55. print(" CORS头部:")
  56. for header in cors_headers:
  57. print(f" {header}")
  58. else:
  59. print(" ⚠️ 响应中未找到CORS头部")
  60. else:
  61. print(f" ❌ 请求失败: {response.text}")
  62. except Exception as e:
  63. print(f" ❌ GET请求失败: {e}")
  64. print()
  65. print("=" * 50)
  66. print("测试完成!")
  67. print("\n如果看到CORS头部,说明配置正确。")
  68. print("如果仍有问题,请检查:")
  69. print("1. Flask应用是否已重启")
  70. print("2. 防火墙设置")
  71. print("3. 代理配置")
  72. if __name__ == "__main__":
  73. test_cors()