Python 定时发送 HTTP 请求,每隔 2 秒请求不同 ID
您可以使用定时器来执行每隔 2 秒发送 http 请求的操作,并在每次请求时更改请求的 id 参数。以下是一个示例代码:
import requests
import time
def send_http_request(id):
url = f'http://example.com?id={id}' # 替换为实际的请求URL
response = requests.get(url)
# 处理返回的响应数据
print(response.text)
def main():
id_list = [1, 2, 3, 4, 5] # 替换为实际的id列表
for id in id_list:
send_http_request(id)
time.sleep(2)
if __name__ == "__main__":
main()
在以上代码中,send_http_request函数用于发送http请求,并传入不同的id参数。main函数中定义了一个id列表,循环遍历列表中的每个id,并在每次循环中调用send_http_request函数发送http请求。然后使用time.sleep(2)函数来使程序暂停2秒,以满足每隔2秒发送一次请求的要求。
您需要将示例代码中的URL和id列表替换为实际的值。另外,请注意在实际的应用中,可能需要处理异常情况和其他错误处理。
原文地址: https://www.cveoy.top/t/topic/o1cI 著作权归作者所有。请勿转载和采集!