Passing socks4/5 connectors to session.<REQUEST> instead of ClientSession(connector=aiosocks.Socks5Addr(...))
Closed this issue · 1 comments
I am trying to create a few thousand async requests using socks4/5 servers.
Instead of passing your connectors to ClientSession(), how to pass the proxy to the session.request directly without recreating sessions for every socks proxy i use, like aiohttp allows passing http proxies. Example-
async with aiohttp.ClientSession() as session: async with session.get("http://python.org", proxy="http://some.proxy.com") as resp:
Alternatively, how can i asynchronously create sessions with different proxies for each request.
Here's my code.
`import asyncio
from aiohttp import ClientSession
import time
async def fetch(i, url, session, proxy):
async with session.get(url, proxy=proxy, timeout=60) as resp:
print(i, resp.status)
data = await resp.json()
print(i, data['origin'])
return data
async def bound_fetch(i, sem, url, session, proxy):
# Getter function with semaphore.
async with sem:
await fetch(i, url, session, proxy)
async def run(r):
url = "https://httpbin.org/get?show_env"
tasks = []
sem = asyncio.Semaphore(1000)
proxy = "http://83.239.58.162:8080"
async with ClientSession() as session:
for i in range(r):
# print(i)
task = asyncio.ensure_future(bound_fetch(i, sem, url, session, proxy))
tasks.append(task)
responses = asyncio.gather(*tasks)
await responses
t1 = time.time()
number = 10000
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(number))
loop.run_until_complete(future)
print("TIME:",time.time() - t1)`
fixed in 0.2.1