Home / Use cases / Python requests
How to Use a Proxy in Python requests
Every way to route requests through a proxy — HTTP, HTTPS, SOCKS5, environment variables, sessions and rotation — with proxies pulled from a free, hourly-verified list.
Basic HTTP / HTTPS proxy
The simplest form — one proxy handles both http and https targets, no extra packages required.
import requests
proxy = "http://1.2.3.4:8080" # a line from the HTTP list
r = requests.get(
"https://httpbin.org/ip",
proxies={"http": proxy, "https": proxy},
timeout=10,
)
print(r.json())
SOCKS5 proxy
Install SOCKS support once, then use the socks5h:// scheme so DNS resolves through the proxy.
pip install "requests[socks]"
proxy = "socks5h://1.2.3.4:1080" # a line from the SOCKS5 list
r = requests.get("https://httpbin.org/ip",
proxies={"http": proxy, "https": proxy}, timeout=10)
Proxy for a whole session
s = requests.Session()
s.proxies = {"http": "http://1.2.3.4:8080", "https": "http://1.2.3.4:8080"}
s.get("https://example.com") # every request in this session uses the proxy
Environment variables
requests also honours the standard proxy environment variables, so you can set a proxy without touching code:
export HTTP_PROXY="http://1.2.3.4:8080" export HTTPS_PROXY="http://1.2.3.4:8080" python your_script.py
Auto-pick a working proxy from the list
import requests
txt = requests.get(
"https://raw.githubusercontent.com/xyzs996/free-proxy-health-list/main/proxies/protocols/http/data.txt",
timeout=15).text
for line in txt.splitlines():
proxy = f"http://{line.strip()}"
try:
r = requests.get("https://httpbin.org/ip",
proxies={"http": proxy, "https": proxy}, timeout=6)
if r.ok:
print("using", proxy, r.json()); break
except requests.RequestException:
continue
Frequently asked questions
Why does my proxy request time out?
Free proxies are volatile — any one can die within minutes. Always set a short timeout and loop to the next proxy on failure. The list is re-verified hourly and sorted fastest-first.
http:// or socks5h:// — which scheme?
Use http:// for HTTP proxies and socks5h:// for SOCKS5. The h makes DNS resolve remotely, which avoids DNS leaks and works better behind restrictive networks.
Can I use these with httpx or aiohttp?
Yes — the same host:port entries work. httpx and aiohttp accept proxy URLs in the same http:// / socks5:// form.