Текстовый файл из учебного комплекта, не свидетельство установленного runtime. Вернуться к учебнику · Исходный файл
1 from contextlib import asynccontextmanager 2 3 import aiohttp 4 5 6 @asynccontextmanager 7 async def model_http_session(*, proxy_url: str, http=None, timeout=None): 8 """Borrow direct sessions; isolate CONNECT from unrelated HTTP clients.""" 9 if http is not None and not proxy_url: 10 yield http 11 return 12 13 connector = None 14 if proxy_url: 15 from aiohttp_socks import ProxyConnector 16 17 # Binary multipart must use the proxy's streaming CONNECT protocol, 18 # not its JSON HTTP request envelope. Never fall back on proxy failure. 19 connector = ProxyConnector.from_url(proxy_url) 20 async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session: 21 yield session