Skip to content

Instantly share code, notes, and snippets.

@ZacharyHampton
Last active May 25, 2022 00:52
Show Gist options
  • Save ZacharyHampton/87cef2454d80e48e45b7333c935a74d9 to your computer and use it in GitHub Desktop.
Save ZacharyHampton/87cef2454d80e48e45b7333c935a74d9 to your computer and use it in GitHub Desktop.
AIOHTTP ClientSession Wrapper (proxy rotate on request)
import aiohttp
from aiohttp import client_exceptions
import os
import random
import time
class CustomClientSession(aiohttp.ClientSession):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
with open(os.getcwd() + '/proxies.txt', 'r') as p:
self.pList = p.read().splitlines()
async def _request(self, *args, **kwargs):
if 'proxy' not in kwargs:
proxy = self.pList[random.randint(0, len(self.pList) - 1)].rstrip()
kwargs['proxy'] = "http://" + proxy
try:
return await super()._request(*args, **kwargs)
except aiohttp.client_exceptions.ClientHttpProxyError:
time.sleep(5)
print('Proxy error, trying again in 5 seconds...')
return await self._request(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment