您可以将
urllib3.util.retry模块与结合使用,
requests以实现以下目的:
from urllib3.util.retry import Retryimport requestsfrom requests.adapters import HTTPAdapterdef retry_session(retries, session=None, backoff_factor=0.3): session = session or requests.Session() retry = Retry( total=retries, read=retries, connect=retries, backoff_factor=backoff_factor, method_whitelist=False, ) adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) return session
用法:
session = retry_session(retries=5)session.post(url=endpoint, data=json.dumps(x), headers=headers)
注意:
您还可以从
Retry类继承并自定义重试行为和重试间隔。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)