Implementing retry for requests in Python

前端 未结 2 891
北荒
北荒 2021-01-04 12:50

How do I implement a retry count of 5 times, 10 seconds apart when sending a POST request using the requests package. I have found plenty of exampl

2条回答
  •  旧时难觅i
    2021-01-04 13:27

    you can use urllib3.util.retry module in combination with requests to have something as follow:

    from urllib3.util.retry import Retry
    import requests
    from requests.adapters import HTTPAdapter
    
    def 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
    

    Usage:

    session = retry_session(retries=5)
    session.post(url=endpoint, data=json.dumps(x), headers=headers)
    

    NB: You can also inherit from Retry class and customize the retry behavior and retry intervals.

提交回复
热议问题