aiohttp使用代理ip访问https网站报错的问题

aiohttp使用代理ip访问https网站报错的问题

昨天遇到个很坑die的问题,当使用aiohttp使用代理访问https网站时候一直报aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host baidu.com:443 ssl:default [参数错误。]这个错误,然后博主在网上一顿搜,最后也没搜出个所以然来,唉,这也是我为什么要把这个坑贴出来的原因,最后在https://stackoverflow.com/这个论坛上才找到了解决办法。

问题

import aiohttp
import asyncio
import threading
from tools import auto

# proxy=auto.proxies, proxy_auth=auto.proxy_auth 这里的代理需要换成自己的
async def quest(url,  headers):
    con = aiohttp.TCPConnector(verify_ssl=False)
    async with aiohttp.ClientSession(connector=con) as sess:
        async with sess.get(url=url, headers=headers, proxy=auto.proxies, proxy_auth=auto.proxy_auth) as res:
            return await res.read()


def forever(loop):
    asyncio.set_event_loop(loop)
    loop.run_forever()


if __name__ == '__main__':
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'}
    url = 'https://baidu.com'

    loop = asyncio.new_event_loop()
    t = threading.Thread(target=forever, args=(loop,))
    t.setDaemon(True)
    t.start()
    ret = asyncio.run_coroutine_threadsafe(quest(url, headers), loop)
    print(ret.result())

然后就出现了这个问题,我用的python版本是3.8.2,aiohttp是3.6.2
在这里插入图片描述

解决

import aiohttp
import asyncio
import threading
from tools import auto
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # 加上这一行
# proxy=auto.proxies, proxy_auth=auto.proxy_auth 这里的代理需要换成自己的
async def quest(url,  headers):
    con = aiohttp.TCPConnector(verify_ssl=False)
    async with aiohttp.ClientSession(connector=con, trust_env=True) as sess: # 加上trust_env=True
        async with sess.get(url=url, headers=headers, proxy=auto.proxies, proxy_auth=auto.proxy_auth) as res:
            return await res.read()


def forever(loop):
    asyncio.set_event_loop(loop)
    loop.run_forever()


if __name__ == '__main__':
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'}
    url = 'https://baidu.com'

    loop = asyncio.new_event_loop()
    t = threading.Thread(target=forever, args=(loop,))
    t.setDaemon(True)
    t.start()
    ret = asyncio.run_coroutine_threadsafe(quest(url, headers), loop)
    print(ret.result())

请求成功
在这里插入图片描述
至于原理我需要再去研究下,或者有带佬知道的欢迎给我留言哈,万分感谢!

  • 11
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值