Python Proxy Authentication: Username:Password Setup for Paid Proxies
Introduction
When running proxies at scale for web scraping and automation tasks, proxy authentication is often necessary to access paid proxy services. This guide delves deep into Python proxy authentication using username and password formats for Bright Data and SmartProxy services. We'll cover proxy URL formats, integrating with popular libraries like requests, httpx, and aiohttp. Additionally, we'll address common 407 errors (proxy authentication required) and provide solutions to fix them.
Proxy URL Formats
The standard format for a proxy URL with username and password is:
http://user:pass@host:port
For Bright Data and SmartProxy, the URLs typically look like this:
Bright Data
http://USERNAME:[email protected]:PORT-REGION-COUNTRY
- USERNAME: Your Bright Data username.
- PASSWORD: Your Bright Data password or zone token.
- PORT: The port number (e.g., 22225).
- REGION: The region code (e.g., us, eu).
- COUNTRY: The country code (e.g., us, gb).
SmartProxy
http://USERNAME:[email protected]:PORT
- USERNAME: Your SmartProxy username.
- PASSWORD: Your SmartProxy password or access token.
- PORT: The port number (e.g., 1080).
Integrating with `requests`
The requests library is straightforward for setting up proxy authentication.
import requests
proxy_url = 'http://USERNAME:[email protected]:22225-us-us'
proxies = {
'http': proxy_url,
'https': proxy_url,
}
response = requests.get('http://httpbin.org/ip', proxies=proxies)
print(response.json())
Integrating with `httpx`
httpx is a modern, async-compatible HTTP client for Python.
import httpx
proxy_url = 'http://USERNAME:[email protected]:22225-us-us'
transport = httpx.HTTPTransport(proxies=proxy_url)
client = httpx.Client(transport=transport)
response = client.get('http://httpbin.org/ip')
print(response.json())
Integrating with `aiohttp`
For asynchronous operations, use the aiohttp library.
import aiohttp
async def fetch(session, url):
async with session.get(url) as response:
return await response.json()
async def main():
proxy_url = 'http://USERNAME:[email protected]:22225-us-us'
connector = aiohttp.TCPConnector(ssl=False)
async with aiohttp.ClientSession(connector=connector, proxy=proxy_url) as session:
response = await fetch(session, 'http://httpbin.org/ip')
print(response)
# Run the main function
import asyncio
asyncio.run(main())
Handling Common 407 Errors
A 407 Proxy Authentication Required error indicates that the proxy server requires authentication. Here are common causes and solutions:
Cause: Incorrect Proxy URL Format
Ensure your proxy URL is correctly formatted with http://user:pass@host:port.
# Correct format
proxy_url = 'http://USERNAME:[email protected]:22225-us-us'
Cause: Expired or Invalid Credentials
Check that your username and password are correct and not expired.
# Double-check credentials
username = 'your_username'
password = 'your_password'
Cause: Firewall or Network Issues
Ensure no firewall is blocking the proxy connection. Test with a different network if possible.
Solution: Use Proxy Authentication Headers
If the above solutions don't work, you can manually set proxy authentication headers.
import requests
proxy_url = 'http://[email protected]:22225-us-us'
proxies = {
'http': proxy_url,
'https': proxy_url,
}
headers = {
'Proxy-Authorization': f'Basic {b64encode(f"{username}:{password}".encode()).decode()}',
}
response = requests.get('http://httpbin.org/ip', proxies=proxies, headers=headers)
print(response.json())
Conclusion
Setting up proxy authentication in Python is essential for accessing paid proxy services like Bright Data and SmartProxy. We covered proxy URL formats, integration with requests, httpx, and aiohttp, and how to handle common 407 errors. If you'd rather not manage proxy lists manually, ProxyForge auto-tests and rotates proxies locally on your machine — one-off $29 unlock, no subscription: https://proxyforge.dev/#download
Free proxies break. ProxyForge doesn't.
The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.
Download ProxyForge free →