# Use a proxy with Python aiohttp

Simultaneous proxy requests are completed using aiohttp ClientSession, proxy authentication, and asynchronous timeout.

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 environment `PROXY_USERNAME` and `PROXY_PASSWORD`.
- First, set the application-side concurrency limit based on the concurrency capability of your plan.

## Complete example

```python
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.
- `ClientSession` Release connection resources at the end of the context.
- Multiple task sharing sessions still require the use of signal volume limits to concur.

## Error handling

- `ClientProxyConnectionError` Prioritize 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.

html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}
