aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host stackoverflow.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)]
Asked Answered
H

11

28

here is my code:

import asyncio
from aiohttp import ClientSession


async def main():
    url = "https://stackoverflow.com/"

    async with ClientSession() as session:
        async with session.get(url) as resp:
            print(resp.status)

asyncio.run(main())

if I run it on my computer, everything works, but if I run it on pythonanywhere, I get this error:

Traceback (most recent call last):
  File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 936, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore  # noqa
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1017, in create_connection
    raise exceptions[0]
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1002, in create_connection
    sock = await self._connect_sock(
  File "/usr/lib/python3.8/asyncio/base_events.py", line 916, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 485, in sock_connect
    return await fut
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 517, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('151.101.193.69', 443)
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test_c.py", line 39, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.8/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete
    return future.result()
  File "test_c.py", line 28, in main
    async with session.get(url, timeout=30) as resp:  # , headers=headers
  File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/client.py", line 1012, in __aenter__
    self._resp = await self._coro
  File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/client.py", line 480, in _request
    conn = await self._connector.connect(
  File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 523, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 858, in _create_connection
    _, proto = await self._create_direct_connection(
  File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 1004, in _create_direct_connection
    raise last_exc
  File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 980, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
  File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 943, in _wrap_create_connection
    raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host stackoverflow.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)]
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f25a71d1a90>

aiohttp on hosting:

Name: aiohttp
Version: 3.6.2
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: [email protected]
License: Apache 2
Location: /home/0dminnimda/.local/lib/python3.8/site-packages
Requires: chardet, async-timeout, multidict, yarl, attrs
Required-by: 

aiohttp on my PC:

Name: aiohttp
Version: 3.6.2
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: [email protected]
License: Apache 2
Location: c:\users\asus\appdata\roaming\python\python38\site-packages
Requires: async-timeout, attrs, chardet, yarl, multidict
Required-by: 

I am at a loss that it is not so? I am running both files using python3.8.

I also tried other urls, they have the same problem

Do I need to add any more details?

Hyla answered 10/8, 2020 at 20:47 Comment(0)
H
29

first solution

Referring to the help from the forum, I added trust_env = True when creating the client and now everything works.

Explanation: Free accounts on PythonAnywhere must use a proxy to connect to the public internet, but aiohttp, by default, does not connect to a proxy accessible from an environment variable.

Link to aiohttp documentation (look for a parameter called "trust_env")

Here is the new code:

import asyncio
from aiohttp import ClientSession


async def main():
    url = "https://stackoverflow.com/"

    async with ClientSession(trust_env=True) as session:
        async with session.get(url) as resp:
            print(resp.status)

asyncio.run(main())

solution if the first didn't help you

The domain you are trying to access must be in whitelist, otherwise you may also get this error.

In this case you need to post a new topic on the pythonanywhere forum asking to add the domain to the whitelist.
If this is an api, then you will need to provide a link to the documentation for this api.

Hyla answered 11/8, 2020 at 18:48 Comment(4)
Seems we had different issues, my solution is related to SSL errors where the installed cert from certifi cannot be located by aiohttp.Pentecostal
@jackotonye, You can, in the same way as I am here, post your question and answer it right away. I think if someone encounters an error like yours, they will be glad to see your solution! (I will be glad to see it too)Hyla
@Pentecostal if the error that you see itself (visually) does not differ much from the one presented in this question, than you shouldn't have deleted the answer, because it may be needed by others who stumble upon this question!Hyla
Yeah you're absolutely right I added the answer to a more related question Thanks https://mcmap.net/q/504111/-getting-quot-ssl-certificate_verify_failed-quot-whenever-i-try-to-log-in-with-my-discord-bot-on-vpsPentecostal
T
12

If you are using Windows OS with python (3.8 or newer) and aiohttp (3.7.4 or older)
Sometimes the solution for an exception like ... Cannot connect to host <REQUESTED URL>:443 ssl:default [The parameter is incorrect] is:

import sys

...

policy = asyncio.WindowsSelectorEventLoopPolicy()
asyncio.set_event_loop_policy(policy)

asyncio.run(main())

And you can check your Python version and OS:

import sys

...

if (sys.platform.startswith('win')
        and sys.version_info[0] == 3
        and sys.version_info[1] >= 8):
    policy = asyncio.WindowsSelectorEventLoopPolicy()
    asyncio.set_event_loop_policy(policy)

asyncio.run(main())

here, in issue 4536, everything is described in more detail.

Thermos answered 12/4, 2021 at 10:21 Comment(2)
Referring to issue 4636, the exception is Cannot connect to host python.or g:443 ssl:default [The parameter is incorrect] and not Cannot connect to host stackoverflow.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)], these are slightly different exceptions. You'd better create a question with exact traceback from issue and answer it.Hyla
Ok, but i think my text was more readebleThermos
C
4

In my case the problem was a very big amount of simultaneously opened connections. To solve the problem I have passed a limit parameter to connector:

import aiohttp
conn = aiohttp.TCPConnector(limit_per_host=5)

async with aiohttp.ClientSession(connector=conn) as session:
Caerphilly answered 20/11, 2022 at 13:3 Comment(0)
D
3

set ssl to False when making the request

import aiohttp
conn = aiohttp.TCPConnector()

async with aiohttp.ClientSession(connector=conn) as session:
    await session.get('https://example.com', ssl=False)
Dupre answered 8/10, 2021 at 10:35 Comment(2)
You should be aware that it is not safe to disable SSL checks. If you don't know what you are doing, then don't use that as a solution.Hyla
Not a great solution. You're running session.get() on an SSL URLBreedlove
P
2

From docs: https://docs.aiohttp.org/en/stable/client_reference.html, params of coroutine async-with request:

ssl – SSL validation mode. None for default SSL check (ssl.create_default_context() is used), False for skip SSL certificate validation, aiohttp.Fingerprint for fingerprint validation, ssl.SSLContext for custom SSL certificate validation. Supersedes verify_ssl, ssl_context and fingerprint parameters.

New in version 3.0.

import asyncio
from aiohttp import ClientSession


async def main():
    url = "https://stackoverflow.com/"

    async with ClientSession() as session:
        async with session.get(url, ssl=False) as resp:
            print(resp.status)

asyncio.run(main())
Paredes answered 8/4, 2022 at 19:28 Comment(0)
P
2

I had the same error on macOS, and it's not aiohttp problem.

Try opening Applications and looking for Install Certificates.command. Run it. This solved the problem on macos.

Proconsul answered 24/10, 2023 at 12:30 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Panek
S
0

If the ClientConnectorCertificateError is due to SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate')] :

You have 3 solutions:

  1. [Recommended approach] Provide SSLContext source:
import ssl
import certifi

sslcontext = ssl.create_default_context(cafile=certifi.where())
async with session.get('https://example.com', ssl=sslcontext) as response:
    .....
  1. Install certificates source:

    1. Navigate to cd /Applications/Python\ 3.11/
    2. Click on Install Certificates.command
  2. [Not recommended] Disable SSL:

import aiohttp
connector=aiohttp.TCPConnector(ssl=False)

async with aiohttp.ClientSession(connector=connector) as session:
    await session.get('https://example.com')
Strophanthus answered 13/11, 2023 at 12:12 Comment(0)
A
0

On macOS this issue occurs when Python is unable to verify the SSL certificate presented by a remote server. This often happens due to missing or outdated root certificates in the system.

Update your certificates using Certifi to fix this problem.

Python can utilize the certifi package to manage SSL certificates.

Install it using:

pip install certifi

and then set the environment variable SSL_CERT_FILE to use the certificate file provided by certifi.

Here's how to do that in Python Code:

import os
import certifi
os.environ['SSL_CERT_FILE'] = certifi.where()
Amiraamis answered 10/12, 2023 at 22:4 Comment(0)
C
0

I have similar problem only in Doker container on VPS server. Error was: aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host apilist.tronscanapi.com:443 ssl:default [None] I checked the possible amount of Maximum Transmission Unit (MTU) transmitted data per cloud server network interface packet. I am attaching the output of the command:

Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg docker0 1500 1093 0 0 0 1078 0 0 0 BMU ens3 1450 2739480 0 0 0 2370890 0 0 0 BMRU It can be seen that one network interface data packet is up to 1450 bytes. At the same time, a Docker network packet can accommodate up to 1500 bytes.

To solve the problem, I adjusted the Docker network interface according to the instructions below: https://www.civo.com/learn/fixing-networking-for-docker

How to fix the MTU issue with Ubuntu. Fortunately the fix is quite easy, you need to add a parameter to your launch script for Docker to specify/override the MTU. Using Ubuntu 22.04 for example, the file to edit as root is /lib/systemd/system/docker.service. You need to change the line looking like this:

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

to look like this:

ExecStart=/usr/bin/dockerd --mtu 1450 -H fd:// --containerd=/run/containerd/containerd.sock

Note: It's quite fine to use the same value as the default network interface (in our example and for our infrastructure 1450), it just can't be higher.

If you are using docker-compose to launch your instances, you will also need to change some config to ensure that they launch with a 1450 MTU.

Your Docker compose file should look something like this:

networks: default: driver: bridge driver_opts: com.docker.network.driver.mtu: 1450

After that, you need to restart Docker and all will be well with your networking:

sudo systemctl daemon-reload

sudo service docker restart

After which requests from the Docker container began to pass correctly.

Craner answered 9/3, 2024 at 9:50 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewKm
L
0

I'm new to Python, and used it in PyCharm with python-binance lib, faced similar problem. After installing and using pyenv the problem gone away.

Also before pyenv I downloaded python package from official site, and there was script to install SSL certificates - maybe that solved the problem

Leelah answered 23/4, 2024 at 19:2 Comment(0)
M
-10

I just solved what could have been a 3 hour problem in 5 mins by changing all https to http. If it's possible, don't use https. I had an issue where another library (playwright) could not use the selector event loop, I would have had to separate process to use aiohttp. Better yet, switch libraries, httpx is an okay alternative.

Massif answered 24/6, 2022 at 19:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.