Python requests 超时 异常捕捉

requests.request 超时异常捕捉

1
2
3
4
5
6
7
response = requests.request(
method,
url,
data,
headers,
(connect_timeout, read_timeout)
)

requests.request 请求超时时,可能抛出多层异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Traceback (most recent call last):
...
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
...
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='xxx', port=80): Read timed out. (read timeout=3)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
...
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='xxx', port=80): Read timed out. (read timeout=3)

异常不用捕获多个,如 urllib3.exceptions.ReadTimeoutError requests.exceptions.ReadTimeout,只要捕获 requests.exceptions.Timeout 即可

带重试的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def request_with_retry(url, payload, method='POST', timeout=(1, 3), retry_times=3):
resp = None
cnt = 0
while cnt < retry_times:
try:
if method in ('get', 'GET'):
response = requests.request(
method,
url,
timeout=timeout,
)
elif method in ('post', 'POST'):
response = requests.request(
method,
url,
data=payload,
timeout=timeout,
)
else:
raise Exception('Unsupported request method: {}'.format(method))
resp = response.json()
break
# 只捕捉 超时异常,其他异常如 json 解析失败等往外抛
except requests.exceptions.Timeout as e:
logger.error(e)
if retry_times - 1 == cnt:
raise Exception('接口 {} 请求超时 {} {} 次'.format(url, timeout, retry_times))
cnt += 1
return resp