> ## Documentation Index
> Fetch the complete documentation index at: https://docs.statproxies.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Requests Library

> Simple proxy configuration for Python's requests library with Stat Proxies.

Python's `requests` library is one of the simplest ways to use Stat Proxies. This guide covers setup, authentication, and common patterns.

## Basic usage

```python theme={null}
import requests

proxies = {
    "http": "http://myuser:mypass@192.168.1.1:3128",
    "https": "http://myuser:mypass@192.168.1.1:3128"
}

response = requests.get("https://httpbin.org/ip", proxies=proxies)
print(response.json())
# {"origin": "192.168.1.1"}
```

<Info>
  Both `http` and `https` keys use the `http://` prefix. This is correct — the proxy server itself accepts HTTP connections, even when forwarding HTTPS traffic.
</Info>

## Using a session

For multiple requests through the same proxy, use a session to reuse connections:

```python theme={null}
import requests

session = requests.Session()
session.proxies = {
    "http": "http://myuser:mypass@192.168.1.1:3128",
    "https": "http://myuser:mypass@192.168.1.1:3128"
}

# All requests through this session use the proxy
response1 = session.get("https://httpbin.org/ip")
response2 = session.get("https://example.com")
```

## Rotating proxies

Distribute requests across multiple proxy IPs:

```python theme={null}
import requests
import random

proxy_list = [
    "http://user1:pass1@192.168.1.1:3128",
    "http://user2:pass2@192.168.1.2:3128",
    "http://user3:pass3@192.168.1.3:3128",
]

def get_with_proxy(url):
    proxy = random.choice(proxy_list)
    proxies = {"http": proxy, "https": proxy}
    return requests.get(url, proxies=proxies)

response = get_with_proxy("https://httpbin.org/ip")
print(response.json())
```

## SOCKS5 support

To use SOCKS5, install the `requests[socks]` extra:

```bash theme={null}
pip install requests[socks]
```

```python theme={null}
import requests

proxies = {
    "http": "socks5://myuser:mypass@192.168.1.1:3128",
    "https": "socks5://myuser:mypass@192.168.1.1:3128"
}

response = requests.get("https://httpbin.org/ip", proxies=proxies)
print(response.json())
```

## Error handling

```python theme={null}
import requests

proxies = {
    "http": "http://myuser:mypass@192.168.1.1:3128",
    "https": "http://myuser:mypass@192.168.1.1:3128"
}

try:
    response = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=10)
    response.raise_for_status()
    print(response.json())
except requests.exceptions.ProxyError:
    print("Proxy connection failed. Check your credentials and proxy status.")
except requests.exceptions.Timeout:
    print("Request timed out.")
except requests.exceptions.RequestException as e:
    print(f"Request error: {e}")
```

<Warning>
  Always set a `timeout` to prevent hanging requests. A value of 10–30 seconds is recommended.
</Warning>

## Related

<CardGroup cols={2}>
  <Card title="Scrapy Integration" icon="spider" href="/articles/scrapy">
    Use proxies with Scrapy's middleware system
  </Card>

  <Card title="Slow Performance" icon="gauge" href="/articles/slow-performance">
    Optimize your proxy configuration for speed
  </Card>
</CardGroup>
