62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import socket
|
|
import requests
|
|
import sys
|
|
import traceback
|
|
|
|
def check_port_open(host, port):
|
|
"""检查指定端口是否开放"""
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
try:
|
|
s.connect((host, port))
|
|
s.shutdown(socket.SHUT_RDWR)
|
|
return True
|
|
except Exception as e:
|
|
return False
|
|
finally:
|
|
s.close()
|
|
|
|
def check_api_status():
|
|
"""检查API服务器状态"""
|
|
try:
|
|
response = requests.get("http://localhost:5000/api/status")
|
|
return response.status_code == 200, response.text
|
|
except Exception as e:
|
|
return False, str(e)
|
|
|
|
def main():
|
|
print("检查服务器状态...")
|
|
|
|
# 检查HTTP服务器端口
|
|
http_port_open = check_port_open("localhost", 5000)
|
|
print(f"HTTP服务器端口 (5000) 状态: {'开放' if http_port_open else '关闭'}")
|
|
|
|
# 检查SMTP服务器端口
|
|
smtp_port_open = check_port_open("localhost", 3825)
|
|
print(f"SMTP服务器端口 (3825) 状态: {'开放' if smtp_port_open else '关闭'}")
|
|
|
|
# 检查API状态
|
|
if http_port_open:
|
|
api_ok, api_response = check_api_status()
|
|
print(f"API状态: {'正常' if api_ok else '异常'}")
|
|
print(f"API响应: {api_response}")
|
|
|
|
# 总结状态
|
|
if http_port_open and smtp_port_open:
|
|
print("\n服务器状态: 正常运行")
|
|
return 0
|
|
else:
|
|
print("\n服务器状态: 异常")
|
|
print("请确保服务器已启动并且端口未被占用。")
|
|
print("启动命令: python run.py --port 5000 --smtp-port 3825 --debug")
|
|
return 1
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
sys.exit(main())
|
|
except Exception as e:
|
|
print(f"检查过程发生错误: {str(e)}")
|
|
traceback.print_exc()
|
|
sys.exit(1) |