Python Proxy Setup with Requests Library: Complete Tutorial
How to configure proxies in Python using the requests library for web scraping and API calls.
NobleProxy 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 requests
proxies = {
'http': 'http://username:password@proxy.nobleproxy.com:8080',
'https': 'http://username:password@proxy.nobleproxy.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.nobleproxy.com:1080',
'https': 'socks5://username:password@proxy.nobleproxy.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 NobleProxy Team
Proxy Experts
The NobleProxy team helps businesses scale their cold outreach with reliable static residential proxies. We share our expertise to help you succeed.