test_cors_new_ip.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. """
  3. 测试新IP地址的CORS配置
  4. 用于验证192.168.3.218:5173的跨域请求是否正常工作
  5. """
  6. import requests
  7. import json
  8. from datetime import datetime
  9. def test_cors_new_ip():
  10. """测试新IP地址的CORS配置"""
  11. base_url = "http://company.citupro.com:5500"
  12. endpoint = "/api/data_parse/get-calendar-info"
  13. print("=== 测试新IP地址的CORS配置 ===")
  14. print(f"测试时间: {datetime.now()}")
  15. print(f"目标服务器: {base_url}")
  16. print(f"测试端点: {endpoint}")
  17. print(f"模拟前端: http://192.168.3.218:5173")
  18. print("=" * 60)
  19. # 测试1: OPTIONS预检请求
  20. print("1. 测试OPTIONS预检请求...")
  21. try:
  22. headers = {
  23. 'Origin': 'http://192.168.3.218:5173',
  24. 'Access-Control-Request-Method': 'GET',
  25. 'Access-Control-Request-Headers': 'Content-Type'
  26. }
  27. response = requests.options(f"{base_url}{endpoint}", headers=headers)
  28. print(f" 状态码: {response.status_code}")
  29. # 检查CORS头部
  30. cors_headers = []
  31. for key, value in response.headers.items():
  32. if key.lower().startswith('access-control'):
  33. cors_headers.append(f"{key}: {value}")
  34. if cors_headers:
  35. print(" CORS头部:")
  36. for header in cors_headers:
  37. print(f" {header}")
  38. else:
  39. print(" ❌ 未找到CORS头部")
  40. except Exception as e:
  41. print(f" ❌ 预检请求失败: {e}")
  42. print()
  43. # 测试2: 实际GET请求
  44. print("2. 测试实际GET请求...")
  45. try:
  46. today = datetime.now().strftime("%Y-%m-%d")
  47. headers = {'Origin': 'http://192.168.3.218:5173'}
  48. response = requests.get(f"{base_url}{endpoint}?date={today}", headers=headers)
  49. print(f" 状态码: {response.status_code}")
  50. if response.status_code == 200:
  51. print(" ✅ 请求成功")
  52. # 检查响应中的CORS头部
  53. cors_headers = []
  54. for key, value in response.headers.items():
  55. if key.lower().startswith('access-control'):
  56. cors_headers.append(f"{key}: {value}")
  57. if cors_headers:
  58. print(" CORS头部:")
  59. for header in cors_headers:
  60. print(f" {header}")
  61. else:
  62. print(" ⚠️ 响应中未找到CORS头部")
  63. else:
  64. print(f" ❌ 请求失败: {response.text}")
  65. except Exception as e:
  66. print(f" ❌ GET请求失败: {e}")
  67. print()
  68. # 测试3: 检查Origin头部设置
  69. print("3. 检查Origin头部设置...")
  70. try:
  71. today = datetime.now().strftime("%Y-%m-%d")
  72. headers = {'Origin': 'http://192.168.3.218:5173'}
  73. response = requests.get(f"{base_url}{endpoint}?date={today}", headers=headers)
  74. origin_header = response.headers.get('Access-Control-Allow-Origin', '未设置')
  75. print(f" Access-Control-Allow-Origin: {origin_header}")
  76. if origin_header == 'http://192.168.3.218:5173':
  77. print(" ✅ Origin头部设置正确")
  78. elif origin_header == 'http://localhost:5173':
  79. print(" ⚠️ Origin头部回退到默认值")
  80. else:
  81. print(f" ❌ Origin头部设置异常: {origin_header}")
  82. except Exception as e:
  83. print(f" ❌ 检查Origin头部失败: {e}")
  84. print()
  85. print("=" * 60)
  86. print("测试完成!")
  87. print("\n如果看到正确的CORS头部,说明配置成功。")
  88. print("如果仍有问题,请检查:")
  89. print("1. Flask应用是否已重启")
  90. print("2. 新的CORS配置是否生效")
  91. print("3. 前端请求的Origin头部是否正确")
  92. if __name__ == "__main__":
  93. test_cors_new_ip()