aiohttp在GET上引发错误,无法将GET请求发送到https网站

2024-06-02 08:15:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个发送GET请求的代码部分:

proxy_auth = aiohttp.BasicAuth('proxy-username', 'proxy-password')
    async with session.get('https://google.com', headers=headers, proxy='http://proxy_url.com', proxy_auth=proxy_auth) as response:
        html = await response.text()

问题是,当我尝试获取https网站时,它会返回以下异常:

ClientConnectorError: Cannot connect to host google.com:443 ssl:default [The parameter is incorrect]

只有当网站由https提供时才会引发此异常,因为http没有异常。 代理是https,但是aiohttp只接受http代理,但是我知道有一些技巧可以让它工作


Tags: 代码httpscomauthhttp代理getaiohttp
1条回答
网友
1楼 · 发布于 2024-06-02 08:15:10

我找到了解决那个问题的办法。发生这种情况是因为aiohttp库本身,所以我们需要将Event Policy Loop更改为asyncio.WindowsSelectorEventLoopPolicy()

就像这样:

import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

相关问题 更多 >