在Python中实现请求的重试

时间:2018-03-05 23:56:21

标签: python python-3.x python-requests

使用POST包发送requests请求时,如何实现5次10秒的重试次数。 我找到了大量GET次请求的示例,而不是post

这就是我目前正在使用的内容,有时我会收到503错误。如果我得到一个错误的响应HTTP代码,我只需要实现重试。

for x in final_payload:
    post_response = requests.post(url=endpoint, data=json.dumps(x), headers=headers)

#Email me the error
if str(post_response.status_code) not in ["201","200"]:
        email(str(post_response.status_code))

2 个答案:

答案 0 :(得分:10)

您可以将urllib3.util.retry模块与requests结合使用,以获得以下内容:

from urllib3.util.retry import Retry
import requests
from requests.adapters import HTTPAdapter

def retry_session(retries, session=None, backoff_factor=0.3, status_forcelist=(500, 502, 503, 504)):
    session = session or requests.Session()
    retry = Retry(
        total=retries,
        read=retries,
        connect=retries,
        backoff_factor=backoff_factor,
        status_forcelist=status_forcelist,
    )
    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)

NB:您还可以从Retry类继承并自定义重试行为并重试间隔。

答案 1 :(得分:2)

我发现Retries的默认行为不适用于POST。为此,需要添加method_whitelist,例如下方:

'''

def retry_session(retries=5):
    session = Session()
    retries = Retry(total=retries,
                backoff_factor=0.1,
                status_forcelist=[500, 502, 503, 504],
                method_whitelist=frozenset(['GET', 'POST']))

    session.mount('https://', HTTPAdapter(max_retries=retries))
    session.mount('http://', HTTPAdapter(max_retries=retries))

    return session

'''