Use a proxy with Python aiohttp
Simultaneous proxy requests are completed using aiohttp ClientSession, proxy authentication, and asynchronous timeout.
Maintained by the ZZHTTP engineering teamUpdated September 20, 2026
aiohttp is suitable for asynchronous tasks and controlled concurrency. The example explicitly passes the proxy and authentication information, not depending on the system proxy configuration of the running machine.
Precondition
- Caller project installs and locks itself
aiohttp. - The account is closed.
us.zzhttp.com:7878; Injection through controlled environmentPROXY_USERNAMEandPROXY_PASSWORD. - First, set the application-side concurrency limit based on the concurrency capability of your plan.
Complete example
import asyncio
import os
import aiohttp
async def main() -> None:
"""Asynchronously check the public exit IP through an explicit HTTP proxy."""
proxy_url = "http://us.zzhttp.com:7878"
proxy_auth = aiohttp.BasicAuth(
os.environ["PROXY_USERNAME"],
os.environ["PROXY_PASSWORD"],
)
timeout = aiohttp.ClientTimeout(total=30, connect=10)
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.get(
"https://ipinfo.io/json",
proxy=proxy_url,
proxy_auth=proxy_auth,
) as response:
response.raise_for_status()
print(await response.json())
asyncio.run(main())
Expected results
- HTTPS requests tunnel through HTTP CONNECT and return to the proxy output.
ClientSessionRelease connection resources at the end of the context.- Multiple task sharing sessions still require the use of signal volume limits to concur.
Error handling
ClientProxyConnectionErrorPrioritize checks for host, port and network availability.- 407 Priority to checking the proxy username and password and not to mis-authenticating the target site.
- Timeouts or connection reset should be limited to a limited evasion and not indefinitely repeated.
Safety tips
Don't take it. proxy_auth The complete exception request or environment variable is output to the log. Different agents should use separate sessions or connection pools to avoid duplicating authentication status across tasks.
Did this answer your question?
Feedback is used only to improve the public documentation and does not submit accounts or request information.