Last active
May 25, 2022 00:52
-
-
Save ZacharyHampton/87cef2454d80e48e45b7333c935a74d9 to your computer and use it in GitHub Desktop.
AIOHTTP ClientSession Wrapper (proxy rotate on request)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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