check_backend.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python3
  2. """
  3. 检查后端服务状态
  4. """
  5. import requests
  6. import json
  7. def check_backend_status():
  8. """检查后端服务状态"""
  9. ports = [8000, 8001, 8002, 8003]
  10. for port in ports:
  11. try:
  12. url = f"http://localhost:{port}"
  13. print(f"检查端口 {port}...")
  14. response = requests.get(url, timeout=5)
  15. if response.status_code == 200:
  16. data = response.json()
  17. print(f"✅ 端口 {port} 服务正常")
  18. print(f" 消息: {data.get('message', 'N/A')}")
  19. print(f" 版本: {data.get('version', 'N/A')}")
  20. # 检查健康状态
  21. try:
  22. health_response = requests.get(f"{url}/health", timeout=5)
  23. if health_response.status_code == 200:
  24. print(f" 健康检查: ✅ 正常")
  25. else:
  26. print(f" 健康检查: ❌ 异常 ({health_response.status_code})")
  27. except:
  28. print(f" 健康检查: ❌ 无响应")
  29. # 检查API文档
  30. try:
  31. docs_response = requests.get(f"{url}/docs", timeout=5)
  32. if docs_response.status_code == 200:
  33. print(f" API文档: ✅ 可访问 ({url}/docs)")
  34. else:
  35. print(f" API文档: ❌ 不可访问")
  36. except:
  37. print(f" API文档: ❌ 无响应")
  38. print(f" 建议前端API地址: {url}")
  39. return port
  40. except requests.exceptions.ConnectionError:
  41. print(f"❌ 端口 {port} 无服务")
  42. except requests.exceptions.Timeout:
  43. print(f"⏰ 端口 {port} 响应超时")
  44. except Exception as e:
  45. print(f"❌ 端口 {port} 检查失败: {e}")
  46. print("\n❌ 未找到可用的后端服务")
  47. print("请先启动后端服务:")
  48. print(" python quick_start.py")
  49. print(" 或")
  50. print(" python test_server.py")
  51. return None
  52. if __name__ == "__main__":
  53. print("=" * 50)
  54. print("后端服务状态检查")
  55. print("=" * 50)
  56. available_port = check_backend_status()
  57. if available_port:
  58. print(f"\n🎉 找到可用服务: http://localhost:{available_port}")
  59. print(f"📚 API文档地址: http://localhost:{available_port}/docs")
  60. print(f"\n📝 前端配置建议:")
  61. print(f" VITE_API_BASE_URL=http://localhost:{available_port}")
  62. else:
  63. print(f"\n🚀 启动后端服务:")
  64. print(f" cd sso-backend")
  65. print(f" python quick_start.py")