# -*- coding: utf-8 -*- """ 诊断Flask应用状态 """ import requests import time import socket def test_port_connection(): """测试端口连接""" print("🔍 测试端口连接...") try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(5) result = sock.connect_ex(('127.0.0.1', 5000)) sock.close() if result == 0: print("✅ 端口5000可以连接") return True else: print("❌ 端口5000无法连接") return False except Exception as e: print(f"❌ 连接测试失败: {e}") return False def test_http_connection(): """测试HTTP连接""" print("\n🌐 测试HTTP连接...") try: # 测试首页 response = requests.get('http://127.0.0.1:5000/', timeout=10) print(f"✅ 首页访问成功,状态码: {response.status_code}") return True except requests.exceptions.ConnectionError as e: print(f"❌ 连接错误: {e}") return False except requests.exceptions.Timeout as e: print(f"❌ 请求超时: {e}") return False except Exception as e: print(f"❌ HTTP测试失败: {e}") return False def test_register_page(): """测试注册页面""" print("\n📝 测试注册页面...") try: response = requests.get('http://127.0.0.1:5000/register', timeout=10) print(f"✅ 注册页面访问成功,状态码: {response.status_code}") return True except requests.exceptions.ConnectionError as e: print(f"❌ 连接错误: {e}") return False except requests.exceptions.Timeout as e: print(f"❌ 请求超时: {e}") return False except Exception as e: print(f"❌ 注册页面测试失败: {e}") return False def main(): print("=" * 60) print("Flask应用诊断") print("=" * 60) # 测试端口连接 port_ok = test_port_connection() if port_ok: # 等待一下让应用完全启动 print("\n⏳ 等待应用启动...") time.sleep(3) # 测试HTTP连接 http_ok = test_http_connection() if http_ok: # 测试注册页面 register_ok = test_register_page() if register_ok: print("\n🎉 所有测试通过!Flask应用运行正常。") else: print("\n⚠️ 注册页面访问失败,但应用基本运行正常。") else: print("\n❌ HTTP连接失败,应用可能有问题。") else: print("\n❌ 端口连接失败,应用可能没有正确启动。") if __name__ == '__main__': main()