python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python异步框架aiohttp无法使用本地代理

解决python异步框架aiohttp无法使用本地代理问题

作者:FOAF-lambda

这篇文章主要介绍了解决python异步框架aiohttp无法使用本地代理问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

问题

import aiohttp
import asyncio
headers = {
        'User-Agent': "mozilla/5.0 (windows nt 6.1; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/69.0.3494.0 safari/537.36",
}
async def fetch(session,url):
    async with session.get(url=url,headers=headers,timeout=50,verify_ssl=False,proxy='http://127.0.0.1:10080') as resposne:
        print(resposne.status)
        return await resposne.text()
async def main():
    async with aiohttp.ClientSession() as session:
        url='https://www.google.com'
        html = await fetch(session,url)
        print(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(main()) 
elif self._trust_env:
    for scheme, proxy_info in proxies_from_env().items():
        if scheme == url.scheme:
            proxy = proxy_info.proxy
            proxy_auth = proxy_info.proxy_auth
            break

解决方式1

解决方式2

def get_local_proxy():
    
    from urllib.request import getproxies
    proxy = getproxies()['http']
    #proxies = {'http': 'http://127.0.0.1:10809', 'https': 'http://127.0.0.1:10809'}
    proxies = {'http': proxy , 'https': proxy}
    return proxies

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文