首页 > 编程语言 >蟒蛇书(Python编程:从入门到实践)第17章使用API 17.1.4处理API响应报错Caused by ProxyError('Unable to connect to proxy&#

蟒蛇书(Python编程:从入门到实践)第17章使用API 17.1.4处理API响应报错Caused by ProxyError('Unable to connect to proxy&#

时间:2024-05-20 20:08:11浏览次数:30  
标签:API chapter lib 17 py Unable 报错 File line

书上提供的原始代码:

import requests

# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"状态码:{r.status_code}")

# 将API响应赋给一个变量
response_dict = r.json()

# 处理结果
print(response_dict.keys())

报错信息:

Traceback (most recent call last):
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connectionpool.py", line 777, in urlopen
    self._prepare_proxy(conn)
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connectionpool.py", line 1046, in _prepare_proxy
    conn.connect()
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connection.py", line 619, in connect
    self.sock = sock = self._connect_tls_proxy(self.host, sock)
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connection.py", line 692, in _connect_tls_proxy
    tls_in_tls=False,
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connection.py", line 793, in _ssl_wrap_socket_and_match_hostname
    tls_in_tls=tls_in_tls,
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\util\ssl_.py", line 471, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\util\ssl_.py", line 515, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
  File "D:\Program\Python\Python37\lib\ssl.py", line 423, in wrap_socket
    session=session
  File "D:\Program\Python\Python37\lib\ssl.py", line 870, in _create
    self.do_handshake()
  File "D:\Program\Python\Python37\lib\ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
OSError: [Errno 0] Error

The above exception was the direct cause of the following exception:

urllib3.exceptions.ProxyError: ('Unable to connect to proxy', OSError(0, 'Error'))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\adapters.py", line 497, in send
    chunked=chunked,
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connectionpool.py", line 846, in urlopen
    method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /search/repositories?q=language:python&sort=stars (Caused by ProxyError('Unable to connect to proxy', OSError(0, 'Error')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Code\pyCharm\chapter_17\python_repos.py", line 6, in <module>
    r = requests.get(url, headers=headers)
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\adapters.py", line 513, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /search/repositories?q=language:python&sort=stars (Caused by ProxyError('Unable to connect to proxy', OSError(0, 'Error')))

解决办法:

修改代码,配置代理服务器

import requests

# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
# 配置代理服务器(注意:具体IP和端口,根据自身情况自行修改)
proxies = {
    'http': 'http://127.0.0.1:7890',
    'https': 'http://127.0.0.1:7890',
}
r = requests.get(url, headers=headers, proxies=proxies)
print(f"状态码:{r.status_code}")

# 将API响应赋给一个变量
response_dict = r.json()

# 处理结果
print(response_dict.keys())

 

标签:API,chapter,lib,17,py,Unable,报错,File,line
From: https://www.cnblogs.com/infocodez/p/18202707

相关文章

  • 随机二次元图片API第三弹
    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解`随机二次元图片API第三弹日期:2020-3-10阿珏折腾代码浏览:3717次评论:27条本来我都把第二弹置顶上来了,没打算在发第三弹的,然后想着想着又憋......
  • 微信JSAPI支付
    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解`微信JSAPI支付日期:2019-3-30阿珏折腾代码浏览:1883次评论:6条前段时间一直在做微信相关的业务,虽说不是什么新技术,但之前一直没有机会接触......
  • Clear Code for Minimal API
    我在写MinimalAPI的时候,发现不能最清晰的看到每个API,原因就是:WebAPI中不断增长逻辑处理过程于是我在想如何简化API至一行,在一点一点想办法中,发现了简化DotNETMinimalAPI的方式。特此记录下来这个思路给需要帮助的人。我的灵感来源于C#11功能-接口中的静态虚拟成员,通过静态......
  • THUWC PKUWC APIO 游记
    MyBlogsTHUSC前情提要:THUWC\(200+10\)参与奖。Day-1坐高铁啦啦啦。身份证落在出租车上,费了很大劲才找回来,感觉很不牛。晚上饥荒启动。Day1进考场前疯狂背诵dwt的sublime编译教程,进考场默写对了/kx/kx。T1唐题,一眼秒了。T2唐题,一眼秒了。T3想了一下,发现状态......
  • 随机二次元图片API第二弹
    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解`随机二次元图片API第二弹日期:2018-3-4阿珏折腾代码浏览:17990次评论:33条时隔差2天三个月,距首个随机二次元图片API发布已有许久时间......
  • Fluent API 配置实体和数据库之间的映射关系
    1.配置主键protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<Blog>().HasKey(b=>b.BlogId);//配置Blog实体的主键为BlogId} 2.配置属性和列protectedoverridevoidOnModelCreating(ModelBuildermodelBu......
  • C++身份证二要素实名认证api、实名认证接口
    在数字化时代背景下,个人信息安全成为了社会关注的焦点。为进一步加强网络空间的安全管理,提升服务效率,身份证实名认证接口的出现为各行业提供了更为安全、高效的身份验证解决方案。随着互联网+政务服务、金融科技、电子商务等领域的快速发展,实名认证需求日益增长。翔云身......
  • Dalsa SDK弹窗报错解决方案-Resource In Use
    问题描述使用Dalsa的CameraLink相机时,代码调用Snap函数和Grab函数时,弹窗报错,详细报错信息是”Errorin"CorXferSelectEX"Resourceinuse“。见下图:解决办法:在每次调用snap函数或者grab函数前,先调用abort函数,将上一帧残留数据强制结束。按照上述办法测试,弹窗报错频率明显较......
  • MySQL报错:SELECT list is not in GROUP BY clause and contains nonaggregated colum
    报错截图解决方法:修改数据库配置1.查看.sql_mode配置select@@global.sql_mode;2.查看返回信息是否包含ONLY_FULL_GROUP_BYONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION;3.去掉ONLY_FULL_GROUP_BY,其他参数不变,执行即可SETGLOBALsql_mode=‘STRI......
  • apisix~集成服务发现注册中心
    摘要当业务量发生变化时,需要对上游服务进行扩缩容,或者因服务器硬件故障需要更换服务器。如果网关是通过配置来维护上游服务信息,在微服务架构模式下,其带来的维护成本可想而知。再者因不能及时更新这些信息,也会对业务带来一定的影响,还有人为误操作带来的影响也不可忽视,所以网关非常......