Python Requests Proxy Setup
Python Requests is the most popular HTTP library in the Python ecosystem, known for its simple API and human-friendly design. It supports proxy configuration natively, making it the fastest way to start using Hex Proxies in a Python script for web scraping, API testing, or data collection.
Basic Configuration with Credentials
proxies = { 'http': 'http://user:pass@gate.hexproxies.com:8080', 'https': 'http://user:pass@gate.hexproxies.com:8080', }
response = requests.get('https://httpbin.org/ip', proxies=proxies, timeout=30) print(response.json()) ```
Using a Session for Multiple Requests
For better performance across multiple requests, use a Session object which reuses the underlying TCP connection:
session = requests.Session()
session.proxies = {
'http': 'http://user:pass@gate.hexproxies.com:8080',
'https': 'http://user:pass@gate.hexproxies.com:8080',# All requests through this session use the proxy r1 = session.get('https://example.com/page1', timeout=30) r2 = session.get('https://example.com/page2', timeout=30) ```
IP Whitelist Authentication
Whitelist your server IP in the Hex Proxies dashboard, then use the proxy without credentials:
proxies = {
'http': 'http://gate.hexproxies.com:8080',
'https': 'http://gate.hexproxies.com:8080',
}SOCKS5 Proxy Support
Install the SOCKS extra and use a SOCKS5 URL:
pip install requests[socks]proxies = {
'http': 'socks5h://user:pass@gate.hexproxies.com:1080',
'https': 'socks5h://user:pass@gate.hexproxies.com:1080',
}Use `socks5h` (with the `h` suffix) to resolve DNS remotely through the proxy, preventing DNS leaks.
Best Practices
- **Always set timeouts** to avoid hanging requests: `timeout=30` is a reasonable default.
- **Rotate user agents** alongside proxy rotation for stronger fingerprint diversity.
- Handle exceptions gracefully — catch `requests.exceptions.ProxyError` and `requests.exceptions.Timeout` separately.
- **Use retries** with the `urllib3.util.Retry` adapter for automatic retry on transient failures.
Troubleshooting
- **ProxyError: Cannot connect**: Verify the proxy host, port, and credentials. Ensure your firewall allows outbound connections on port 8080.
- **407 responses**: Authentication failed. Check username/password or verify your IP is whitelisted.
- **SSLError**: Ensure your Python environment has up-to-date certificates. Try `pip install certifi`.
- **Slow responses**: Residential proxies add 1-3 seconds of latency. This is normal. Increase timeout values accordingly.