Skip to content

Instantly share code, notes, and snippets.

@iKlotho
Created July 25, 2020 22:08
Show Gist options
  • Save iKlotho/aaf0235dce3f535a67f10352690f9a22 to your computer and use it in GitHub Desktop.
Save iKlotho/aaf0235dce3f535a67f10352690f9a22 to your computer and use it in GitHub Desktop.
aiohttp pass proxy to ClientSession
from aiohttp.client import ClientSession
class CustomClientSession(ClientSession):
"""
Use proxy on every request when passed to Client
Usage
>> async with CustomClientSession(proxy="http://1.1.1.1:3128") as session:
"""
def __init__(self, *args, **kwargs):
if 'proxy' in kwargs:
self.proxy = kwargs.pop('proxy')
super().__init__(
*args, **kwargs
)
async def _request(self, *args, **kwargs):
if hasattr(self, 'proxy'):
kwargs.update({
"proxy": self.proxy
})
return await super()._request(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment