出现错误“ConnectionError: Max retries exceeded with url”有多种原因:
request.get()
方法传递了不正确或不完整的 URL。确保我们指定了正确且完整的 URL 和路径。
# ⛔️ 未指定协议 (https://)
example.com/posts# ✅ 完整网址的示例
https://example.com/posts
仔细检查我们没有在 URL 和路径中输入任何错误。
解决该错误的一种方法是使用 Retry
对象并指定要重试多少次与连接相关的错误,并设置在两次尝试之间应用的退避因子。
import requests
from requests.adapters import HTTPAdapter, Retrydef make_request():session = requests.Session()retry = Retry(connect=3, backoff_factor=0.5)adapter = HTTPAdapter(max_retries=retry)session.mount('http://', adapter)session.mount('https://', adapter)url = 'https://example.com/api/users'response = session.get(url)parsed = response.json()print(parsed)make_request()
Session 对象允许我们跨请求保留某些参数。
我们将以下关键字参数传递给 Retry 对象:
上面的示例在第二次尝试后以 0.5 秒的退避因子重试请求 3 次。
如果不想在发生错误时重试,也可以使用 try/except
块。
import requestsdef make_request():try:url = 'https://example.com/api/users'response = requests.get(url, timeout=30)parsed = response.json()print(parsed)except requests.exceptions.ConnectionError:# 👇️ 在此处处理错误或使用 `pass` 语句print('connection error occurred')make_request()
如果 try
块中出现连接错误,except
块将运行。
如果由于请求无法验证站点的 SSL 证书而收到错误,您可以将验证关键字参数设置为 False 以禁用请求的 SSL 证书验证。
请注意
,我们应该只在本地开发或测试期间禁用 SSL 证书验证,因为它可能会使我们的应用程序容易受到中间人攻击。
import requestsdef make_request():try:url = 'https://example.com/api/users'# 👇️ 将验证设置为 Falseresponse = requests.get(url, verify=False, timeout=30)parsed = response.json()print(parsed)except Exception as e:print(e)make_request()
另一种解决方案是使用 time.sleep()
方法在请求之间设置一定的延迟。
API 可能会限制我们的请求,这可能不会延迟发生。
from time import sleep
import requestsdef make_request():try:url = 'https://example/api/users'response = requests.get(url, timeout=30)parsed = response.json()print(parsed['data'][0])except requests.exceptions.ConnectionError:# 👇️ 在此处处理错误或使用 `pass` 语句print('connection error occurred')for i in range(3):make_request()sleep(1.5)
time.sleep
方法暂停执行给定的秒数。
代码示例以 1.5 秒的延迟向 API 发出 3 个请求。
我们还可以使用 while 循环重复请求,直到服务器响应。
from time import sleep
import requestsresponse = Nonewhile response is None:try:url = 'https://example.com/api/users'response = requests.get(url, timeout=30)breakexcept:print('Connection error occurred')sleep(1.5)continueprint(response)
parsed = response.json()
print(parsed)
我们使用 while
循环每 1.5 秒发出一次请求,直到服务器无连接错误地响应。
try
语句尝试向 API 发出 HTTP 请求,如果请求失败,except 块将在我们暂停执行 1.5 秒的地方运行。
continue
语句用于继续 while 循环的下一次迭代。
重复该过程,直到 HTTP 请求成功并使用 break 语句。
我们还可以在代码的错误处理部分更加具体。
from time import sleep
import requestsresponse = Nonewhile response is None:try:url = 'https://example.com/api/users'response = requests.get(url, timeout=30)breakexcept requests.ConnectionError as e:print('Connection error occurred', e)sleep(1.5)continueexcept requests.Timeout as e:print('Timeout error - request took too long', e)sleep(1.5)continueexcept requests.RequestException as e:print('General error', e)sleep(1.5)continueexcept KeyboardInterrupt:print('The program has been canceled')print(response)
parsed = response.json()
print(parsed)
requests.ConnectionError
错误意味着我们的站点或服务器上存在连接问题。
检查我们的互联网连接并确保服务器可以访问互联网。
requests.Timeout
错误在请求花费的时间太长时引发(在示例中超过 30 秒)。
requests.RequestException
错误是一个通用的、包罗万象的错误。
当用户取消程序时会引发 KeyboardInterrupt
异常,例如 按 CTRL + C。
要解决错误“ConnectionError: Max retries exceeded with url”,请确保:
request.get()
时指定正确且完整的 URL。