Python 中错误 ConnectionError: Max retries exceeded with url
迪丽瓦拉
2024-05-29 13:11:04
0

出现错误“ConnectionError: Max retries exceeded with url”有多种原因:

  1. request.get() 方法传递了不正确或不完整的 URL。
  2. 我们正受到 API 的速率限制。
  3. requests 无法验证您向其发出请求的网站的 SSL 证书。

确保我们指定了正确且完整的 URL 和路径。

# ⛔️ 未指定协议 (https://)
example.com/posts# ✅ 完整网址的示例
https://example.com/posts

仔细检查我们没有在 URL 和路径中输入任何错误。

使用 Retry 对象进行回退重试

解决该错误的一种方法是使用 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 对象:

  • connect - 要重试的与连接相关的错误数
  • backoff_factor - 第二次尝试后在两次尝试之间应用的退避因子。

上面的示例在第二次尝试后以 0.5 秒的退避因子重试请求 3 次。


使用 try/except 语句在发生错误时不重试

如果不想在发生错误时重试,也可以使用 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 证书验证

如果由于请求无法验证站点的 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() 方法实现请求之间的延迟

另一种解决方案是使用 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。
  • 不受 API 的速率限制。
  • requests 模块能够验证站点的 SSL 证书。
  • 可以访问互联网。

相关内容