Skip to content

Commit 970c5d9

Browse files
authored
add proxy and proxy_auth varriables to ClientSession.__init__ (#9207)
1 parent c240b52 commit 970c5d9

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

CHANGES/9207.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added ``proxy`` and ``proxy_auth`` parameters to ``ClientSession`` -- by :user:`meshya`.

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ Matthieu Hauglustaine
243243
Matthieu Rigal
244244
Matvey Tingaev
245245
Meet Mangukiya
246+
Meshya
246247
Michael Ihnatenko
247248
Michał Górny
248249
Mikhail Burshteyn

aiohttp/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ class ClientSession:
252252
"_max_line_size",
253253
"_max_field_size",
254254
"_resolve_charset",
255+
"_default_proxy",
256+
"_default_proxy_auth",
255257
)
256258

257259
def __init__(
@@ -261,6 +263,8 @@ def __init__(
261263
connector: Optional[BaseConnector] = None,
262264
cookies: Optional[LooseCookies] = None,
263265
headers: Optional[LooseHeaders] = None,
266+
proxy: Optional[StrOrURL] = None,
267+
proxy_auth: Optional[BasicAuth] = None,
264268
skip_auto_headers: Optional[Iterable[str]] = None,
265269
auth: Optional[BasicAuth] = None,
266270
json_serialize: JSONEncoder = json.dumps,
@@ -361,6 +365,9 @@ def __init__(
361365

362366
self._resolve_charset = fallback_charset_resolver
363367

368+
self._default_proxy = proxy
369+
self._default_proxy_auth = proxy_auth
370+
364371
def __init_subclass__(cls: Type["ClientSession"]) -> None:
365372
raise TypeError(
366373
"Inheritance class {} from ClientSession "
@@ -480,6 +487,11 @@ async def _request(
480487
for i in skip_auto_headers:
481488
skip_headers.add(istr(i))
482489

490+
if proxy is None:
491+
proxy = self._default_proxy
492+
if proxy_auth is None:
493+
proxy_auth = self._default_proxy_auth
494+
483495
if proxy is not None:
484496
try:
485497
proxy = URL(proxy)

docs/client_advanced.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,13 @@ Authentication credentials can be passed in proxy URL::
605605
session.get("http://python.org",
606606
proxy="http://user:[email protected]")
607607

608+
And you may set default proxy::
609+
610+
proxy_auth = aiohttp.BasicAuth('user', 'pass')
611+
async with aiohttp.ClientSession(proxy="http://proxy.com", proxy_auth=proxy_auth) as session:
612+
async with session.get("http://python.org") as resp:
613+
print(resp.status)
614+
608615
Contrary to the ``requests`` library, it won't read environment
609616
variables by default. But you can do so by passing
610617
``trust_env=True`` into :class:`aiohttp.ClientSession`

tests/test_client_session.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,55 @@ async def test_proxy_str(session: ClientSession, params: _Params) -> None:
710710
]
711711

712712

713+
async def test_default_proxy(loop: asyncio.AbstractEventLoop) -> None:
714+
proxy_url = URL("http://proxy.example.com")
715+
proxy_auth = mock.Mock()
716+
proxy_url2 = URL("http://proxy.example2.com")
717+
proxy_auth2 = mock.Mock()
718+
719+
class OnCall(Exception):
720+
pass
721+
722+
request_class_mock = mock.Mock(side_effect=OnCall())
723+
session = ClientSession(
724+
proxy=proxy_url, proxy_auth=proxy_auth, request_class=request_class_mock
725+
)
726+
727+
assert session._default_proxy == proxy_url, "`ClientSession._default_proxy` not set"
728+
assert (
729+
session._default_proxy_auth == proxy_auth
730+
), "`ClientSession._default_proxy_auth` not set"
731+
732+
with pytest.raises(OnCall):
733+
await session.get(
734+
"http://example.com",
735+
)
736+
737+
assert request_class_mock.called, "request class not called"
738+
assert (
739+
request_class_mock.call_args[1].get("proxy") == proxy_url
740+
), "`ClientSession._request` uses default proxy not one used in ClientSession.get"
741+
assert (
742+
request_class_mock.call_args[1].get("proxy_auth") == proxy_auth
743+
), "`ClientSession._request` uses default proxy_auth not one used in ClientSession.get"
744+
745+
request_class_mock.reset_mock()
746+
with pytest.raises(OnCall):
747+
await session.get(
748+
"http://example.com", proxy=proxy_url2, proxy_auth=proxy_auth2
749+
)
750+
751+
assert request_class_mock.called, "request class not called"
752+
assert (
753+
request_class_mock.call_args[1].get("proxy") == proxy_url2
754+
), "`ClientSession._request` uses default proxy not one used in ClientSession.get"
755+
assert (
756+
request_class_mock.call_args[1].get("proxy_auth") == proxy_auth2
757+
), "`ClientSession._request` uses default proxy_auth not one used in ClientSession.get"
758+
759+
await session.close()
760+
761+
713762
async def test_request_tracing(
714763
loop: asyncio.AbstractEventLoop, aiohttp_client: AiohttpClient
715764
) -> None:

0 commit comments

Comments
 (0)