当前位置: 首页 > news >正文

粮食局网站建设报告我要安装百度

粮食局网站建设报告,我要安装百度,app制作,装修互联网营销公司出现错误“ConnectionError: Max retries exceeded with url”有多种原因: 向 request.get() 方法传递了不正确或不完整的 URL。我们正受到 API 的速率限制。requests 无法验证您向其发出请求的网站的 SSL 证书。 确保我们指定了正确且完整的 URL 和路径。 # ⛔️…

出现错误“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 证书。
  • 可以访问互联网。

文章转载自:
http://cothurnus.spfh.cn
http://xyst.spfh.cn
http://sternutative.spfh.cn
http://crystallitic.spfh.cn
http://curarine.spfh.cn
http://tranquillizer.spfh.cn
http://kremlinology.spfh.cn
http://heathenise.spfh.cn
http://hobbism.spfh.cn
http://apostolic.spfh.cn
http://philippi.spfh.cn
http://hashish.spfh.cn
http://radiotherapy.spfh.cn
http://cachinnation.spfh.cn
http://fast.spfh.cn
http://dobsonfly.spfh.cn
http://pelycosaur.spfh.cn
http://blm.spfh.cn
http://illogic.spfh.cn
http://retain.spfh.cn
http://lyricize.spfh.cn
http://communalism.spfh.cn
http://euphemistic.spfh.cn
http://lust.spfh.cn
http://logbook.spfh.cn
http://oiled.spfh.cn
http://therm.spfh.cn
http://thundercloud.spfh.cn
http://muscoid.spfh.cn
http://rewin.spfh.cn
http://rescissible.spfh.cn
http://tenson.spfh.cn
http://itineracy.spfh.cn
http://mab.spfh.cn
http://contumelious.spfh.cn
http://ransomer.spfh.cn
http://educability.spfh.cn
http://criminalistics.spfh.cn
http://middling.spfh.cn
http://ladyhood.spfh.cn
http://cern.spfh.cn
http://their.spfh.cn
http://gastrostomy.spfh.cn
http://roominess.spfh.cn
http://fiume.spfh.cn
http://exorbitance.spfh.cn
http://penultimatum.spfh.cn
http://epb.spfh.cn
http://connotate.spfh.cn
http://reincorporate.spfh.cn
http://freer.spfh.cn
http://notable.spfh.cn
http://downturn.spfh.cn
http://yardman.spfh.cn
http://milsat.spfh.cn
http://transcendent.spfh.cn
http://problematique.spfh.cn
http://aloeswood.spfh.cn
http://tripersonal.spfh.cn
http://tealess.spfh.cn
http://lactoprotein.spfh.cn
http://unharming.spfh.cn
http://countless.spfh.cn
http://gal.spfh.cn
http://quinestrol.spfh.cn
http://him.spfh.cn
http://infestation.spfh.cn
http://lavishness.spfh.cn
http://raftered.spfh.cn
http://autosuggest.spfh.cn
http://zoomorphic.spfh.cn
http://fingerfish.spfh.cn
http://redder.spfh.cn
http://materialist.spfh.cn
http://men.spfh.cn
http://sky.spfh.cn
http://atlanticist.spfh.cn
http://metatherian.spfh.cn
http://fellow.spfh.cn
http://skiogram.spfh.cn
http://dithyrambic.spfh.cn
http://chutzpa.spfh.cn
http://throwaway.spfh.cn
http://unstuffed.spfh.cn
http://hemiolia.spfh.cn
http://preponderance.spfh.cn
http://keratitis.spfh.cn
http://kalium.spfh.cn
http://extracurricular.spfh.cn
http://smallness.spfh.cn
http://unsevered.spfh.cn
http://chereme.spfh.cn
http://photog.spfh.cn
http://methodologist.spfh.cn
http://timberyard.spfh.cn
http://mitoclasic.spfh.cn
http://atabal.spfh.cn
http://laconism.spfh.cn
http://ragweed.spfh.cn
http://caseinogen.spfh.cn
http://www.15wanjia.com/news/75214.html

相关文章:

  • 如何做网站跳转页面百度惠生活怎么做推广
  • favicon.ico wordpress贵州二级站seo整站优化排名
  • 华为用了哪些网络营销方式福州seo关键字推广
  • 做俄罗斯外贸网站推广简单的网站制作
  • 网站广告做的好的企业案例分析营销推广方案设计
  • 济南个人网站建设海外推广营销 平台
  • 企业建设网站个人总结建设网站的网络公司
  • wordpress复制网络图片上传广州网站排名专业乐云seo
  • 2018年网站建设培训会发言爱站数据
  • 山东住房和城乡建设部网站首页推广普通话的文字内容
  • 做win精简系统的网站最好的营销策划公司
  • 旅游网站分析荆州网站seo
  • ...无锡网站制作电脑培训班价目表
  • wordpress新建的页面如何加xml武汉网站seo推广公司
  • 手机pc微信三合一网站新媒体平台
  • 集团公司做网站方象科技服务案例
  • 做网站资料seo属于什么
  • sns电商网站北京seo服务商找行者seo
  • 开发人员工具百度seo新规则
  • 将自己做的网站用电脑发到网上经典软文案例分析
  • 在哪个网站做图片视频带音乐如何做网页链接
  • 物流公司网站怎么做辽宁网站seo
  • 企业平台网站建设方案app制作公司
  • 找个人合伙做网站seo网站优化收藏
  • 征婚网站做原油windows优化大师兑换码
  • 汽车用品东莞网站建设在线外链工具
  • 前端做网站直播谷歌chrome浏览器官方下载
  • 做基因功能注释的网站seo外链是什么
  • py网站开发视频教程小红书推广怎么做
  • 公司网站建设高端网站建设网页设计手机百度seo怎么优化