如何使用SOCKS代理通过aiohttp发出请求?(How to use SOCKS proxies to make requests with aiohttp?)

import asyncio

import aiohttp
from aiosocksy.connector import ProxyConnector, ProxyClientRequest


async def fetch(url):
    connector = ProxyConnector()
    socks = 'socks5://127.0.0.1:12377'
    async with aiohttp.ClientSession(connector=connector, request_class=ProxyClientRequest) as session:
        async with session.get(url, proxy=socks) as response:
            print(await response.text())


loop = asyncio.get_event_loop()
loop.run_until_complete(fetch('https://www.google.com'))

  

posted @ 2020-10-14 01:35  John-Python  阅读(1550)  评论(0编辑  收藏  举报