Setup Guides · 1 min read
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 · Updated
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'
ProductStatic residentialOne dedicated proxy per account on a real home connection, with the same IP for the full term.Details ProductRotating residentialA new home IP on every request or sticky sessions up to 24 hours, in 116 countries.Details
#Python #requests #programming