Python Proxy Setup with Requests Library: Complete Tutorial
How to configure proxies in Python using the requests library for web scraping and API calls.
OutreachProxy Team
Proxy Experts
Python Requests Proxy Configuration
The requests library makes proxy configuration straightforward.
TL;DR: Pass a proxies dictionary to requests.get() with http and https keys containing your proxy URLs.
Basic Setup
import requestsproxies = {
'http': 'http://username:password@proxy.outreachproxy.com:8080',
'https': 'http://username:password@proxy.outreachproxy.com:8080'
}
response = requests.get('https://httpbin.org/ip', proxies=proxies)
print(response.json())
SOCKS5 Proxy
Install PySocks first:
pip install requests[socks]
Then:
proxies = {
'http': 'socks5://username:password@proxy.outreachproxy.com:1080',
'https': 'socks5://username:password@proxy.outreachproxy.com:1080'
}
Session-Based Usage
session = requests.Session()
session.proxies = proxies# All requests use the proxy
response = session.get('https://example.com')
Error Handling
try:
response = requests.get(url, proxies=proxies, timeout=10)
except requests.exceptions.ProxyError:
print("Proxy connection failed")
except requests.exceptions.Timeout:
print("Request timed out")
Environment Variables
Alternatively:
import os
os.environ['HTTP_PROXY'] = 'http://proxy:port'
os.environ['HTTPS_PROXY'] = 'http://proxy:port'
Written by OutreachProxy Team
Proxy Experts
The OutreachProxy team helps businesses scale their cold outreach with reliable static residential proxies. We share our expertise to help you succeed.