python全栈

python request 超时重试

import requests
import json
from requests.adapters import HTTPAdapter

s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=3))
s.mount('https://', HTTPAdapter(max_retries=3))
url1 = "http://uc.api.xxxxxx.com/api/app/access_token?app_id=xxxxx&app_secret=xxxx"
r1 = s.get(url1)
token = json.loads(r1.text)['data']['access_token']
print(token)
url2 = "https://uc.api.xxxxx.com/api/app/permission/xxxxxx/access_module"
params = { "access_token": token}
try:
    r2 = s.get(url2, timeout=5,  params=params)
    print(r2.text)
except requests.exceptions.RequestException as e:
    print(e)

留言

您的电子邮箱地址不会被公开。 必填项已用*标注

闽ICP备20008591号-1