Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Getting "SSL: Certificate_verify_failed" whenever I try to log in with my Discord bot on VPS

I'm moving my bot from my old, broken laptop to a proper VPS. I'm on an older async version of Discord.py (0.16.0), because I started work on this thing a long time before the rewrite was a thing; and I don't have much experience with Linux, so moving to a Windows Server seemed prudent. I've installed all of the same packages (as far as I know), but I'm consistently getting an error on startup, on the bot.run() line:

ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

The error happens during the handshake, so far as I can tell from the stacktrace, not that I'd know what the implications of that are.

This setup has worked for several years on two different laptops (albeit running Windows 10) without any issues, and the discord.py server doesn't seem to have encountered such issues on a Windows machine (it happens on OS X, but because of some idiosyncrasy of Python 3.6, and this was also happening in Python 3.5. Wrong OS, too.) I tried changing Python installs and installing some of the automatic Windows updates, along with running some commands I found here and there: pip install certifi and pip install incremental.

After several hours of nothing out of curiosity: and frustration and owing to the fact that I have a test bot on one server that I don't mind being trashed, I tried from something I saw in a stackoverflow discussion

bot = commands.Bot(
    command_prefix='/',
    connector=aiohttp.TCPConnector(verify_ssl=False)
        ) 

(the added line is specifically the whole connector kwarg, which was not there before.)

The odd thing is, this CHANGED the stacktrace, but didn't actually change the error, as shown below:

Before adding verify_ssl = False, I was getting...

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 601, in _create_direct_connection
    local_addr=self._local_addr)
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 802, in create_connection
    sock, protocol_factory, ssl, server_hostname)
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 828, in _create_connection_transport
    yield from waiter
  File "C:\Program Files\Python36\lib\asyncio\sslproto.py", line 503, in data_received
    ssldata, appdata = self._sslpipe.feed_ssldata(data)
  File "C:\Program Files\Python36\lib\asyncio\sslproto.py", line 201, in feed_ssldata
    self._sslobj.do_handshake()
  File "C:\Program Files\Python36\lib\ssl.py", line 683, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

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

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 304, in connect
    yield from self._create_connection(req)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 578, in _create_connection
    transport, proto = yield from self._create_direct_connection(req)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 624, in _create_direct_connection
    (req.host, req.port, exc.strerror)) from exc
aiohttp.errors.ClientOSError: [Errno 1] Can not connect to discordapp.com:443 [[SSL: CERTIFICATE_VERIFY_FAILED] certific
ate verify failed (_ssl.c:749)]

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

Traceback (most recent call last):
  File "harubotFE98.py", line 43748, in <module>    # trust me, I know
    bot.run('SNIP')
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 519, in run
    self.loop.run_until_complete(self.start(*args, **kwargs))
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 466, in run_until_complete
    return future.result()
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 490, in start
    yield from self.login(*args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 416, in login
    yield from getattr(self, '_login_' + str(n))(*args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 346, in _login_1
    data = yield from self.http.static_login(token, bot=is_bot)
  File "C:\Program Files\Python36\lib\site-packages\discord\http.py", line 195, in static_login
    data = yield from self.get(self.ME)
  File "C:\Program Files\Python36\lib\site-packages\discord\http.py", line 105, in request
    r = yield from self.session.request(method, url, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 555, in __iter__
    resp = yield from self._coro
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 198, in _request
    conn = yield from self._connector.connect(req)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 314, in connect
    .format(key, exc.strerror)) from exc
aiohttp.errors.ClientOSError: [Errno 1] Cannot connect to host discordapp.com:443 ssl:True [Can not connect to discordap
p.com:443 [[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)]]
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x00000224005401D0>

but after, it was shortened(?) to...

Traceback (most recent call last):
  File "harubotFE98.py", line 43748, in <module>
    bot.run('NOPE')
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 519, in run
    self.loop.run_until_complete(self.start(*args, **kwargs))
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 466, in run_until_complete
    return future.result()
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 491, in start
    yield from self.connect()
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 444, in connect
    self.ws = yield from DiscordWebSocket.from_client(self)
  File "C:\Program Files\Python36\lib\site-packages\discord\gateway.py", line 175, in from_client
    ws = yield from websockets.connect(gateway, loop=client.loop, klass=cls)
  File "C:\Program Files\Python36\lib\site-packages\websockets\py35\client.py", line 19, in __await__
    return (yield from self.client)
  File "C:\Program Files\Python36\lib\site-packages\websockets\client.py", line 150, in connect
    factory, wsuri.host, wsuri.port, **kwds)
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 802, in create_connection
    sock, protocol_factory, ssl, server_hostname)
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 828, in _create_connection_transport
    yield from waiter
  File "C:\Program Files\Python36\lib\asyncio\sslproto.py", line 503, in data_received
    ssldata, appdata = self._sslpipe.feed_ssldata(data)
  File "C:\Program Files\Python36\lib\asyncio\sslproto.py", line 201, in feed_ssldata
    self._sslobj.do_handshake()
  File "C:\Program Files\Python36\lib\ssl.py", line 683, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000021A50E0E358>

I've also tried messing with something called OpenSSL, but pretty much any discussion I can find on it is dense enough that I can't quite wrap my head around it.

Edit: After some messing around, I've written something of a minimum script to get the error to trigger:

import aiohttp
import asyncio
import ssl

async def main():
    async with aiohttp.ClientSession() as cs:
        async with cs.get("https://www.discordapp.com") as r:
            res = await r.text()
            print(res)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

which returns:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 924, in _wrap_create_connection
    await self._loop.create_connection(*args, **kwargs))
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 802, in create_connection
    sock, protocol_factory, ssl, server_hostname)
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 828, in _create_connection_transport
    yield from waiter
  File "C:\Program Files\Python36\lib\asyncio\sslproto.py", line 503, in data_received
    ssldata, appdata = self._sslpipe.feed_ssldata(data)
  File "C:\Program Files\Python36\lib\asyncio\sslproto.py", line 201, in feed_ssldata
    self._sslobj.do_handshake()
  File "C:\Program Files\Python36\lib\ssl.py", line 683, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

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

Traceback (most recent call last):
  File "test2.py", line 12, in <module>
    loop.run_until_complete(main())
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 466, in run_until_complete
    return future.result()
  File "test2.py", line 7, in main
    async with cs.get("https://www.discordapp.com") as r:
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 1005, in __aenter__
    self._resp = await self._coro
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 476, in _request
    timeout=real_timeout
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 522, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 854, in _create_connection
    req, traces, timeout)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 992, in _create_direct_connection
    raise last_exc
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 974, in _create_direct_connection
    req=req, client_error=client_error)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\connector.py", line 929, in _wrap_create_connection
    raise ClientConnectorSSLError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorSSLError: Cannot connect to host www.discordapp.com:443 ssl:None [[SSL: CERTIFI
CATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)]

I've also tried a couple of other solutions, such as using a web client to visit discordapp.com:443 (which worked, but did not fix this) and updating python's aiohttp to the most recent build (which did not fix this).

Answer*

Draft saved
Draft discarded

Required fields are marked with *

Cancel
1
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. Commented Jan 23, 2022 at 15:20

lang-py