python - 在 Python 中实现请求 api 的重试功能,无需任何库
问题描述
我正在尝试建立一个疫苗可用性项目。我每 4 秒请求一次检查疫苗剂量,
response = requests.get(https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=770&date=)
有时我会遇到Connection Error
. 我想在我的代码中添加重试功能,我知道那里有很多答案,但他们正在使用该库。我想构建自己的重试功能来了解核心机制并获得乐趣。
任何人都可以帮助它应该如何实施?
解决方案
import requests
import time
def foo():
try:
response = response.get("some URL")
# you can modify exceptions as per your choice
except Exception as e:
time.sleep(30)
foo()
推荐阅读
- java - 如何加载一个可以从其他类访问和操作的 ArrayList,而无需多次加载它们?
- javascript - 如何在数组中添加一个切换为活动的元素?
- antlr - antlr4 的小问题:text==null,不知道为什么
- knn - 当我运行下面的代码时,我收到一个错误,因为“索引 0 超出了轴 0 大小为 0 的范围”
- c - 写入文件。创建文件但不写入文件
- python - 使用资源模块导入数据文件
- android - 将颤振项目发布到 Play 商店后,Firebase Auth 登录不起作用
- sql - 在分层查询中确定 (root), (root+1), (root+2)
- flowtype - 为反应组件包装子组件,该组件的子组件仅限于某些类型
- gnuplot - gnuplot pm3d 将第二列映射为颜色渐变