# Use a proxy with Python Requests

Use requests to read environment variables, set HTTP proxies, timeout, and perform limited error handling.

Example of using the caller-side project self-managed `requests` Dependency. The account is closed. `us.zzhttp.com:7878` The full proxy username and sub-username and password are read from the controlled environment.

## Python requests

```python
import os
from urllib.parse import quote

import requests

proxy_username = quote(os.environ["PROXY_USERNAME"], safe="")
proxy_password = quote(os.environ["PROXY_PASSWORD"], safe="")
proxy_url = f"http://{proxy_username}:{proxy_password}@us.zzhttp.com:7878"
proxies = {"http": proxy_url, "https": proxy_url}

response = requests.get(
    "https://ipinfo.io/json",
    proxies=proxies,
    timeout=(10, 30),
)
response.raise_for_status()
print(response.json())
```

## Dependencies and Configuration

- Install and lock dependencies in the project virtual environment.
- The proxy value does not enter the source code or mirror layer.

## Request

- Both HTTP and HTTPS targets use the same HTTP proxy URL.
- Set the connection and read timeouts separately.

## Error handling

- Distinguish between agent errors, connection timeouts, and target statuses.
- Only limited retries are performed on parity requests.

## Verification steps

1. Locking in a virtual environment `requests` Versions and injected through deployment systems `PROXY_USERNAME` and `PROXY_PASSWORD`.
2. Run one agentless request to record local exports, then run the sample and read the response `ip`, `country` and `region`; Region results are from third-party databases.
3. Simulates the wrong gateway port, wrong password and short timeout, respectively, to confirm that the exception type can be distinguished and the log does not contain the proxy dictionary.

## Expected results

- `raise_for_status()` Without exceptions, the proxy request returns an output that is different from the local public IP.
- Invalid password `requests.exceptions.ProxyError` or equivalent `407` The response, the connection timeout, and the target HTTP error remain distinguishable.

## Failure border

- Missing environment variables are thrown before a network request is initiated `KeyError` This is not a proxy availability error.
- The example covers only a single HTTP request. High concurrency, connection pools, retry, and power-equal boundaries should be designed business-specific.

## Security

- The exception log does not output the proxy dictionary.
- The response body is recorded in the required fields.

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 .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 pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}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);}
