以下是一个简单的示例代码,演示了如何不断调用API方法来检查状态:
import time
import requests
def check_status(api_url):
response = requests.get(api_url)
status = response.json().get('status')
return status
def continuously_check_status(api_url):
while True:
status = check_status(api_url)
print(f"Current status: {status}")
if status == 'completed':
break
time.sleep(5) # 每隔5秒重新检查一次状态
api_url = 'https://example.com/api/status'
continuously_check_status(api_url)
在上面的代码中,check_status
函数发送一个GET请求到指定的API URL,并返回API返回的状态。continuously_check_status
函数不断调用check_status
函数来检查状态,如果状态为"completed",则停止检查。
注意:在实际情况中,你需要根据你的API的具体情况来修改代码。具体来说,你需要替换api_url
变量为你要调用的API的URL,并根据API的返回结果来修改check_status
函数的逻辑。