使用python重试失败的HTTP请求

时间:2016-01-21 18:25:52

标签: python python-requests

我从内部API请求JSON,但偶尔API会重载并返回{u'message': u'INTERNAL_ERROR'}

我想继续重试请求,直到成功为止。我不确定它是如何重复的。正如我的代码现在一样,它只会重试一次。

r = requests.get(self.time_series_endpoint, params=payload)

json = r.json()

if json.get('message') == 'INTERNAL_ERROR':
    r = requests.get(self.time_series_endpoint, params=payload)
    json = r.json()
    json = json['buckets']
else:
    json = json['buckets']

1 个答案:

答案 0 :(得分:2)

while json.get('message') == 'INTERNAL_ERROR':
    r = requests.get(self.time_series_endpoint, params=payload)
    json = r.json()
json = json['buckets']