定时调外部任务开启api
This commit is contained in:
43
test_api.py
Normal file
43
test_api.py
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
"""测试API调用脚本"""
|
||||
import requests
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
def test_api_call():
|
||||
"""测试API调用"""
|
||||
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
api_url = "https://jsonplaceholder.typicode.com/posts"
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': 'Celery-Task/1.0'
|
||||
}
|
||||
payload = {
|
||||
'title': f'API测试 - {current_time}',
|
||||
'body': f'这是测试API调用的数据',
|
||||
'userId': 1
|
||||
}
|
||||
|
||||
try:
|
||||
print(f"正在测试API调用: {api_url}")
|
||||
response = requests.post(api_url, headers=headers, json=payload, timeout=30)
|
||||
|
||||
if response.status_code == 201:
|
||||
result_data = response.json()
|
||||
print(f"✅ API调用成功")
|
||||
print(f"响应数据: {json.dumps(result_data, indent=2, ensure_ascii=False)}")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ API调用失败 - 状态码: {response.status_code}")
|
||||
print(f"响应内容: {response.text}")
|
||||
return False
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ API调用异常: {str(e)}")
|
||||
return False
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("=== API调用测试 ===")
|
||||
success = test_api_call()
|
||||
print(f"\n测试结果: {'成功' if success else '失败'}")
|
||||
Reference in New Issue
Block a user