Python’s requests library is one of the simplest ways to use Stat Proxies. This guide covers setup, authentication, and common patterns.
Basic usage
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"}
Both http and https keys use the http:// prefix. This is correct — the proxy server itself accepts HTTP connections, even when forwarding HTTPS traffic.
Using a session
For multiple requests through the same proxy, use a session to reuse connections:
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:
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:
pip install requests[socks]
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
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 } " )
Always set a timeout to prevent hanging requests. A value of 10–30 seconds is recommended.
Scrapy Integration Use proxies with Scrapy’s middleware system
Slow Performance Optimize your proxy configuration for speed