aiohttp.client_exceptions.ClientConnectorError:无法连接到主机stackoverflow.com:443 ssl:default [连接调用失败('151.101.193.69',443)] [英] aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host stackoverflow.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)]

查看:3705
本文介绍了aiohttp.client_exceptions.ClientConnectorError:无法连接到主机stackoverflow.com:443 ssl:default [连接调用失败('151.101.193.69',443)]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

import asyncio
import aiohttp


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

    async with aiohttp.ClientSession(loop=loop) as session:
        async with session.get(url, timeout=30) as resp:
            print(resp.status)

asyncio.run(main())

如果我在计算机上运行它,则一切正常,但是如果我在pythonanywhere上运行,则会出现此错误:

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>

1-托管上的aiohttp,2-我的个人计算机上的aiohttp

1 - aiohttp on hosting, 2 - 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: fafhrd91@gmail.com
License: Apache 2
Location: /home/0dminnimda/.local/lib/python3.8/site-packages
Requires: chardet, async-timeout, multidict, yarl, attrs
Required-by: 

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: fafhrd91@gmail.com
License: Apache 2
Location: c:\users\asus\appdata\roaming\python\python38\site-packages
Requires: async-timeout, attrs, chardet, yarl, multidict
Required-by: 

我很茫然,不是吗?我正在使用python3.8运行两个文件.

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?

推荐答案

引用从论坛获得帮助,我在创建客户端时添加了trust_env = True,现在一切正常.这是新代码:

referring to the help from the forum, I added trust_env = True when creating the client and now everything works. here is the new code:

import asyncio
import aiohttp


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

    async with aiohttp.ClientSession(loop=loop, trust_env=True) as session:
        async with session.get(url, timeout=30) as resp:
            print(resp.status)

asyncio.run(main())

这篇关于aiohttp.client_exceptions.ClientConnectorError:无法连接到主机stackoverflow.com:443 ssl:default [连接调用失败('151.101.193.69',443)]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆