aiohttp.client_exceptions.ClientConnectorError:无法连接到主机***.com:443 ssl:默认[连接调用失败('151.101.193.69',443
Posted
技术标签:
【中文标题】aiohttp.client_exceptions.ClientConnectorError:无法连接到主机***.com:443 ssl:默认[连接调用失败(\'151.101.193.69\',443)]【英文标题】:aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host ***.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)]aiohttp.client_exceptions.ClientConnectorError:无法连接到主机***.com:443 ssl:默认[连接调用失败('151.101.193.69',443)] 【发布时间】:2020-11-30 12:11:20 【问题描述】:这是我的代码:
import asyncio
from aiohttp import ClientSession
async def main():
url = "https://***.com/"
async with ClientSession() as session:
async with session.get(url) as resp:
print(resp.status)
asyncio.run(main())
如果我在我的计算机上运行它,一切正常,但如果我在 pythonanywhere 上运行它,我会收到以下错误:
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 ***.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)]
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f25a71d1a90>
aiohttp 托管:
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:
我的电脑上的 aiohttp:
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 运行这两个文件。
我也试过其他网址,他们有同样的问题
我需要添加更多细节吗?
【问题讨论】:
【参考方案1】:发出请求时将 ssl 设置为 False
import aiohttp
conn = aiohttp.TCPConnector()
async with aiohttp.ClientSession(connector=conn) as session:
await session.get('https://example.com', ssl=False)
【讨论】:
您应该知道禁用 SSL 检查是不安全的。如果您不知道自己在做什么,请不要将其用作解决方案。【参考方案2】:如果您使用 Windows 操作系统 和 python(3.8 或更高版本)和 aiohttp(3.7.4 或更低版本)
有时像... Cannot connect to host <REQUESTED URL>:443 ssl:default [The parameter is incorrect]
这样的异常的解决方案是:
import sys
...
policy = asyncio.WindowsSelectorEventLoopPolicy()
asyncio.set_event_loop_policy(policy)
asyncio.run(main())
您可以检查您的 Python 版本和操作系统:
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,一切都描述得更详细了。
【讨论】:
参考 issue 4636,例外是Cannot connect to host python.or g:443 ssl:default [The parameter is incorrect]
而不是Cannot connect to host ***.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)]
,这些是略有不同的例外。您最好创建一个具有确切回溯的问题并回答它。
好的,但我认为我的文字更易读【参考方案3】:
第一个解决方案
参考help from the forum,我在创建客户端时添加了trust_env = True
,现在一切正常。
说明: PythonAnywhere 上的免费帐户必须使用代理连接到公共互联网,但默认情况下,aiohttp 不会连接到可从环境变量访问的代理。
Link to aiohttp documentation(查找名为“trust_env”的参数)
这是新代码:
import asyncio
from aiohttp import ClientSession
async def main():
url = "https://***.com/"
async with ClientSession(trust_env=True) as session:
async with session.get(url) as resp:
print(resp.status)
asyncio.run(main())
如果第一个没有帮助您的解决方案
您尝试访问的域必须在whitelist,否则您也可能会收到此错误。
在这种情况下,您需要post a new topic on the pythonanywhere forum 要求将域添加到白名单。 如果这是一个 api,那么您需要提供此 api 文档的链接。
【讨论】:
似乎我们遇到了不同的问题,我的解决方案与 SSL 错误有关,即 aiohttp 无法找到从 certifi 安装的证书。 @jackotonye,您可以像我在这里一样,发布您的问题并立即回答。我想如果有人遇到像你这样的错误,他们会很高兴看到你的解决方案! (我也会很高兴看到它) @jackotonye 如果您看到的错误本身(视觉上)与此问题中提出的错误没有太大区别,那么您不应该删除答案,因为其他人可能需要它偶然发现这个问题! 是的,你说得对,我添加了一个更相关问题的答案谢谢***.com/a/66842057/4584672以上是关于aiohttp.client_exceptions.ClientConnectorError:无法连接到主机***.com:443 ssl:默认[连接调用失败('151.101.193.69',443的主要内容,如果未能解决你的问题,请参考以下文章
aiohttp.client_exceptions.ClientConnectorError:无法连接到主机***.com:443 ssl:默认[连接调用失败('151.101.193.69',443