# Configure TrueProxies with Python

> Configure TrueProxies with Python requests and aiohttp. Set up HTTP or SOCKS5 proxies, authentication, timeouts and Residential IPv4 sessions.

Source: https://docs.trueproxies.com/integrations/python/

> **Use your own endpoints**
>
> The examples below use placeholders. Copy the connection host, ports, username, and proxy password from the **Username and password** tab of your service in the dashboard. See [How to connect](https://docs.trueproxies.com/proxy-instructions/how-to-connect/#connection-host-and-ports).

## Overview

TrueProxies works with standard Python HTTP clients over HTTP CONNECT and SOCKS5. You can authenticate with your proxy username and password or with a trusted IP.

## Installation

Install the common client libraries:

```bash
pip install requests "requests[socks]" aiohttp aiohttp-socks
```

You will need:

- proxy host (`YOUR_HOST` below)
- HTTP port (`YOUR_HTTP_PORT` below)
- SOCKS5 port (`YOUR_SOCKS_PORT` below)
- proxy username
- proxy password

Copy all five from your service in the dashboard.

For Residential IPv4 country targeting or sticky sessions, append the documented parameters to the proxy username before using it in code. Do not reuse those parameters for Residential IPv6 or Datacenter IPv6.

## HTTP proxy

```python
import requests

username = "your_username"
password = "your_password"
proxy_url = f"http://{username}:{password}@YOUR_HOST:YOUR_HTTP_PORT"

response = requests.get(
    "https://httpbin.org/ip",
    proxies={
        "http": proxy_url,
        "https": proxy_url,
    },
    timeout=30,
)

print(response.status_code)
print(response.text)
```

## SOCKS5 proxy

```python
import requests

username = "your_username"
password = "your_password"
proxy_url = f"socks5h://{username}:{password}@YOUR_HOST:YOUR_SOCKS_PORT"

response = requests.get(
    "https://httpbin.org/ip",
    proxies={
        "http": proxy_url,
        "https": proxy_url,
    },
    timeout=30,
)

print(response.json())
```

## Async with aiohttp

```python
import asyncio

from aiohttp import ClientSession
from aiohttp_socks import ProxyConnector

username = "your_username"
password = "your_password"
connector = ProxyConnector.from_url(
    f"http://{username}:{password}@YOUR_HOST:YOUR_HTTP_PORT"
)


async def main() -> None:
    async with ClientSession(connector=connector) as session:
        async with session.get("https://httpbin.org/ip", timeout=30) as response:
            print(response.status)
            print(await response.text())


asyncio.run(main())
```

## Error handling

- Set explicit request timeouts so failed proxy handshakes do not hang workers.
- If you receive authentication failures, re-check your proxy username, password, and any trusted IPs you rely on.
- If you use Residential IPv4 username parameters for targeting or sticky sessions, log the final username string you send so you can reproduce failures.
- For payment or account-state issues, check the dashboard for billing alerts or suspended-account notices before assuming the proxy endpoint is unhealthy.

## Related guides

- [Connection and username format](https://docs.trueproxies.com/proxy-instructions/how-to-connect/)
- [HTTP CONNECT and SOCKS5](https://docs.trueproxies.com/proxy-instructions/proxy-protocols/)
- [Trusted IPs](https://docs.trueproxies.com/proxy-instructions/trusted-ips/)
- [Troubleshooting checklist](https://docs.trueproxies.com/support/)
