提问者:小点点

Python Discord Bot aiohttp。客户例外。客户端连接器错误异常


在Python3.6上使用此代码。6:

import discord


client = discord.Client()


@client.event
async def on_ready():
    print('Logged in!')

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('/'):
        await message.channel.send('Got command')

client.run('Njk1*******************************************************')

我最终得到了一个很长的回溯:

Traceback(最近的调用最后):文件/库/框架/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiohttp/connector.py,第936行,_wrap_create_connection返回等待自己。_loop.create_connection(*args,**kwargs)#类型:忽略#noqa文件/库/框架/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py,第804行,create_connection袜子,protocol_factory,ssl,server_hostname)文件/库/框架/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py,第830行,_create_connection_transport从服务员产生文件/库/框架/Python.framework/Versions/3.6/lib/python3.6/asyncio/sslproto.py,第505行,data_receivedssldata,appdata=自己。_sslpipe.feed_ssldata(数据)文件"/库/框架/Python.framework/Versions/3.6/lib/python3.6/asyncio/sslproto.py",第201行,feed_ssldata自己。_sslobj.do_handshake()文件"/库/框架/Python.framework/Versions/3.6/lib/python3.6/ssl.py",行689,do_handshake。_sslobj.do_handshake()ssl。SSLError:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl. c: 841)

上述异常是以下异常的直接原因:

Traceback (most recent call last):
  File "/Users/eric/Desktop/ Python_Files/lib/discordbot/discordbot.py", line 21, in <module>
    client.run('Njk1*******************************************************')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 640, in run
    return future.result()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 621, in runner
    await self.start(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 584, in start
    await self.login(*args, bot=bot)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 442, in login
    await self.http.static_login(token.strip(), bot=bot)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/http.py", line 261, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/http.py", line 165, in request
    async with self.__session.request(method, url, **kwargs) as r:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiohttp/client.py", line 1012, in __aenter__
    self._resp = await self._coro
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiohttp/client.py", line 483, in _request
    timeout=real_timeout
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiohttp/connector.py", line 523, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiohttp/connector.py", line 859, in _create_connection
    req, traces, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiohttp/connector.py", line 1004, in _create_direct_connection
    raise last_exc
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiohttp/connector.py", line 986, in _create_direct_connection
    req=req, client_error=client_error)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiohttp/connector.py", line 941, in _wrap_create_connection
    raise ClientConnectorSSLError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorSSLError: Cannot connect to host discordapp.com:443 ssl:default [[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:841)]

有人能帮我解决这个问题吗?这可能是我的系统的问题,因为我在网上搜索了一些其他的教程,没有一个有效,我的互联网连接也很好。(我不能代码阻止异常的第一部分,因为它有太多的代码)


共1个答案

匿名用户

证书未安装,因此我运行了安装证书。命令,并修复了证书错误。以下是文件内容:

#!/bin/sh

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 << "EOF"

# install_certifi.py
#
# sample script to install or update a set of default Root Certificates
# for the ssl module.  Uses the certificates provided by the certifi package:
#       https://pypi.org/project/certifi/

import os
import os.path
import ssl
import stat
import subprocess
import sys

STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
             | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
             | stat.S_IROTH |                stat.S_IXOTH )

def main():
    openssl_dir, openssl_cafile = os.path.split(
        ssl.get_default_verify_paths().openssl_cafile)

    print(" -- pip install --upgrade certifi")
    subprocess.check_call([sys.executable,
        "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])

    import certifi

    # change working directory to the default SSL directory
    os.chdir(openssl_dir)
    relpath_to_certifi_cafile = os.path.relpath(certifi.where())
    print(" -- removing any existing file or link")
    try:
        os.remove(openssl_cafile)
    except FileNotFoundError:
        pass
    print(" -- creating symlink to certifi certificate bundle")
    os.symlink(relpath_to_certifi_cafile, openssl_cafile)
    print(" -- setting permissions")
    os.chmod(openssl_cafile, STAT_0o775)
    print(" -- update complete")

if __name__ == '__main__':
    main()
EOF