重试请求

时间:2016-01-10 22:52:39

标签: python python-requests

如果响应状态代码不是200,我正在尝试重试请求,所以我写了这个函数:

    def _get_resource(url, max_retries=0, delay=0):
         number_retries = 0
         response = requests.get(url)
         while response.status_code != 200 and number_retries < max_retries:
            time.sleep(delay)
            response = requests.get(url)
            number_retries += 1
        response.raise_for_status()
        return response.content

有任何改进建议吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用HTTPAdapter请求。以下内容应该有效:

import time
import httplib

import requests
from requests.adapters import HTTPAdapter


class RetryHTTPAdapter(HTTPAdapter):

    SECONDS_BETWEEN_RETRIES = 5

    def __init__(self, retry_time=120, *args, **kwargs):
        self.retry_time = retry_time
        super(RetryHTTPAdapter, self).__init__(*args, **kwargs)

    def send(self, *args, **kwargs):
        for _ in range(int(self.retry_time / self.SECONDS_BETWEEN_RETRIES)):
            response = super(RetryHTTPAdapter, self).send(*args, **kwargs)
            if response.status_code == httplib.OK:
                break
            time.sleep(self.SECONDS_BETWEEN_RETRIES)
        return response

s = requests.Session()
s.mount('http://', RetryHTTPAdapter(retry_time=180))
s.mount('https://', RetryHTTPAdapter(retry_time=180))

s.get('http://example.com')