当前位置:网站首页>解决ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
解决ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
2022-04-22 06:27:00 【三天打鱼,两天晒网】
问题
原代码:
async def call_wss_api(msg):
async with websockets.connect('wss://xxx.com/tool/handle') as websocket:
await websocket.send(msg)
response = ""
count = 0
while websocket.open:
response = await websocket.recv()
return response
执行代码时,发现会报错:ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)
Traceback (most recent call last):
File "/Users/aaa/xxx.py", line 354, in create_order_by_athena
res = asyncio.get_event_loop().run_until_complete(call_wss_api(msg))
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
return future.result()
File "/Users/aaa/xxx.py", line 376, in call_wss_api
async with websockets.connect('wss://xxx.com/tool/handle') as websocket:
File "/Users/aaa/xxx/venv/lib/python3.10/site-packages/websockets/legacy/client.py", line 633, in __aenter__
return await self
File "/Users/aaa/xxx/venv/lib/python3.10/site-packages/websockets/legacy/client.py", line 650, in __await_impl_timeout__
return await asyncio.wait_for(self.__await_impl__(), self.open_timeout)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/tasks.py", line 445, in wait_for
return fut.result()
File "/Users/aaa/xxx/venv/lib/python3.10/site-packages/websockets/legacy/client.py", line 654, in __await_impl__
transport, protocol = await self._create_connection()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 1089, in create_connection
transport, protocol = await self._create_connection_transport(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 1119, in _create_connection_transport
await waiter
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/sslproto.py", line 534, in data_received
ssldata, appdata = self._sslpipe.feed_ssldata(data)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/sslproto.py", line 188, in feed_ssldata
self._sslobj.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 974, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)
进程已结束,退出代码为 1
解决方法
需要在调用函数websockets.connect()时,传递一个关键字参数ssl=ssl_context即可,代码如下:
import ssl
import certifi
ssl_context = ssl.create_default_context()
ssl_context.load_verify_locations(certifi.where())
async def call_wss_api(msg):
async with websockets.connect('wss://xxx.com/tool/handle', ssl=ssl_context) as websocket:
await websocket.send(msg)
response = ""
count = 0
while websocket.open:
response = await websocket.recv()
return response
版权声明
本文为[三天打鱼,两天晒网]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u014259820/article/details/124304325
边栏推荐
- 可视化编程——实验一
- Stm32外设篇 [四] RCC
- 依赖冲突查找与解决办法(以EasyPoi为例,出现 NoSuchMethodError 或 NoClassDefFoundError )
- Unity interview questions (follow-up)
- Oracle sequence usage collation
- 树莓派mono上跨平台运行一个C#自制的简易图片处理器
- ActiveX control usage summary
- 获取当前dll或者exe路径
- [TCP / IP overview]
- Multithreading (threaded Communication [producer and consumer])
猜你喜欢
随机推荐
【TCP/IP 一 概述】
Inverse Polish quaternion
单片机原理[一] 学好单片机必会的五张图
Google cache cleanup plug-in
树莓派:访问Bitlocker To Go加密过的磁盘
C-10 最大公约数、最小公倍数
[communication interface can bus]
【GPS - NMEA-0183协议】
可视化编程——绘图篇作业
Unity基础知识
RT thread [i] create project
Multithreading (threaded Communication [producer and consumer])
树莓派Lite:安装discuz最新版
Get the current DLL or exe path
“可视化程序设计”试卷
可视化编程——实验一
MySQL 中文字段排序问题(根据中文拼音排序)
树莓派4:自定义网络时间来源
依赖冲突查找与解决办法(以EasyPoi为例,出现 NoSuchMethodError 或 NoClassDefFoundError )
QT学习汇总






![Multithreading (threaded Communication [producer and consumer])](/img/bc/f1bb196f5c4bf9771d5dbef00e2936.png)


