解决ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

这篇具有很好参考价值的文章主要介绍了解决ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

问题

原代码:

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://www.cnpython.com/qa/260804文章来源地址https://www.toymoban.com/news/detail-588455.html

到了这里,关于解决ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 如何解决SSL: CERTIFICATE_VERIFY_FAILED

    \\\"SSL: CERTIFICATE_VERIFY_FAILED\\\"错误通常在使用Python的requests或urllib等库进行HTTPS请求时出现,它表明SSL证书验证失败。这可能是由于服务器证书无效、过期、自签名或缺失等原因所致。要解决此问题,可以尝试以下方法: 参考: http://www.howtouselinux.com/post/ssl-certificate_verify_failed-in-

    2024年02月16日
    浏览(30)
  • requests出现ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)解决方案

      大家好,我是爱编程的喵喵。双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中。从事机器学习以及相关的前后端开发工作。曾在阿里云、科大讯飞、CCF等比赛获得多次Top名次。现为CSDN博客专家、人工智能领域优质创作者。喜欢通过博客创作

    2024年02月11日
    浏览(32)
  • 解决python报错[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed:

    这个错误经常发生在在线下载代码, 模型文件或数据的时候。发生这个错误的原因是没有正确配置网络的证书相关的东西。 解决办法: 全局取消证书验证 参考: https://www.jianshu.com/p/7d8eee279e7d

    2024年02月10日
    浏览(29)
  • “URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed“问题解决

    在使用Docker进行深度学习环境搭建时,出现此类报错,故寻找解决方法。 urlopen https时需要验证SSL的证书,当网络使用自签名的证书会报错。 在Python脚本中,添加下列代码 再次运行,问题解决。 在使用Docker进行深度学习环境搭建时,出现SSL报错,解决方法如上。

    2024年02月11日
    浏览(28)
  • Python单接口请求和接口自动化框架请求解决Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAIL

    项目实践总结2种处理SSLError的办法,下面就详细说明一下 接口请求示例如下: 导入urllib3,导包下面写上urllib3.disable_warnings(),在请求中加上verify=False。 data: request_data: method: GET url: - https://xxx/open/pick/codeId?signMode=1clientId=af901bbe8b9f0f3eebc3db46aba8d209signTimestamp=signTimesignData=signData bo

    2024年02月08日
    浏览(40)
  • repo init报error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

    在ubuntu执行repo init的时候报了repo init报error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed这种错误,解决方法是需要更新本地证书。问题现象以及解决方法如下所示: 安装证书 更新本地证书

    2024年02月05日
    浏览(32)
  • python https urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

    Python 2.7.9 之后引入了一个新特性,当使用urllib.urlopen打开一个 https 链接时,会验证一次 SSL 证书。而当目标网站使用的是自签名的证书时就会抛出一个 urllib2.URLError: urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed 的错误 解决方案 1.局部处理方案 使用ssl创建未经验证的

    2024年02月05日
    浏览(27)
  • python3/pip3 SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

    mac os 电脑之前安装的是python3.9 , 现在升级到python3.10。 从python官网下载macos版本的python3.10 pkg。 双击安装。 程序使用aiohttp访问ebay 。 但是旧版本python执行程序时不会出现以上问题。猜是新python的环境没配置好。 进入python 3.10 的application安装路径。 执行证书安装命令: 安装

    2024年02月11日
    浏览(28)
  • python安装第三方包异常【SSLEOFError】、[SSL: CERTIFICATE_VERIFY_FAILED]

    Could not fetch URL https://pypi.org/simple/ xxxx /: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=\\\'pypi.org\\\', port=443): Max retries exceeded with url: /simple/ xxxx l/ (Caused by SSLError(SSLEOFError(8, \\\'EOF occurred in violation of protocol (_ssl.c:852)\\\'),)) - skipping ERROR: Could not find a version that satisfies the requi

    2024年02月11日
    浏览(18)
  • python爬虫ssl.SSLCertVerificationError解决办法

    python当使用requests.get获取https链接时,提示如下错误: ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056) 方法一: 最方便的手段是直接设置不验证,即: 方法二: 全局取消证书验证,简易方便,但对整个项目可能会

    2024年02月15日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包