Created
July 25, 2020 22:08
-
-
Save iKlotho/aaf0235dce3f535a67f10352690f9a22 to your computer and use it in GitHub Desktop.
aiohttp pass proxy to ClientSession
This file contains hidden or 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
| 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