67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
测试 TaskReporter API 连接
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import time
|
|
import datetime
|
|
import json
|
|
import requests
|
|
from requests.adapters import HTTPAdapter
|
|
from requests.packages.urllib3.util.retry import Retry
|
|
|
|
def test_api_connection():
|
|
print("开始测试 API 连接...")
|
|
|
|
# 创建会话
|
|
session = requests.Session()
|
|
session.mount('http://', HTTPAdapter(max_retries=3))
|
|
session.mount('https://', HTTPAdapter(max_retries=3))
|
|
|
|
# 服务器 URL
|
|
SERVER_URL = 'https://toolstrack.cgnico.com'
|
|
|
|
# 准备测试数据
|
|
test_data = {
|
|
'username': os.environ.get('USERNAME'),
|
|
'tool_name': 'MetaBox_Test',
|
|
'task_name': 'API_Connection_Test',
|
|
'time_saved': "1.5", # 确保是字符串格式
|
|
'time_cost': "0.2", # 确保是字符串格式
|
|
'timestamp': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 使用文档中指定的时间格式
|
|
}
|
|
|
|
print(f"测试数据: {json.dumps(test_data, ensure_ascii=False, indent=2)}")
|
|
|
|
try:
|
|
print(f"正在连接 {SERVER_URL}/api.php...")
|
|
response = session.post(
|
|
f'{SERVER_URL}/api.php',
|
|
json=test_data,
|
|
timeout=5 # 增加超时时间以确保有足够时间响应
|
|
)
|
|
|
|
print(f"状态码: {response.status_code}")
|
|
print(f"响应内容: {response.text}")
|
|
|
|
if response.status_code == 200:
|
|
print("API 连接成功!")
|
|
return True
|
|
else:
|
|
print(f"API 连接失败,状态码: {response.status_code}")
|
|
return False
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
print(f"请求异常: {str(e)}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"其他异常: {str(e)}")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
test_api_connection()
|