Python Requests Proxy: How to Use a Proxy with Requests

To use a proxy with Python requests, pass a proxies dictionary that maps both http and https to your proxy address. That is the whole idea. The details that trip people up are around it: how to include a password with special characters, why the https key still uses http://, how environment variables can quietly override your setting, and what each proxy error actually means. This guide covers all of them with copy-ready code.

New to proxies? See what a proxy server is first.

The basic setup

import requests

proxy = "http://user:pass@proxy-host:port"
proxies = {"http": proxy, "https": proxy}

r = requests.get("https://api.ipify.org", proxies=proxies, timeout=30)
print(r.text)   # should print the proxy's address, not yours

Replace user, pass, proxy-host and port with the details from your provider. If your provider uses an IP allow-list instead of a password, leave out the user:pass@ part.

Why “https” points to an http:// proxy

The keys in the dictionary ("http", "https") describe the website you are visiting. The value describes how to talk to the proxy.

For an HTTPS website, requests asks the HTTP proxy to open a tunnel, and the encrypted conversation passes through it. So the proxy address normally starts with http:// even for the https key. Writing https://proxy-host tells requests the proxy itself speaks HTTPS, which most proxies do not — a common cause of SSL errors.

If you only set the "http" key, every https:// request goes out without the proxy. Always set both.

Username, password and special characters

If your password contains characters like @, :, # or %, they break the address. Encode them first:

from urllib.parse import quote

user = quote("my-user", safe="")
pwd  = quote("p@ss:word#1", safe="")
proxy = f"http://{user}:{pwd}@proxy-host:port"

Keep credentials out of your code where you can — read them from environment variables or a config file that is not committed to version control:

import os
proxy = f"http://{os.environ['PROXY_USER']}:{os.environ['PROXY_PASS']}@proxy-host:port"

Using a Session

If you send many requests, a Session reuses connections (faster) and lets you set the proxy once:

s = requests.Session()
s.proxies.update({"http": proxy, "https": proxy})

for url in urls:
    r = s.get(url, timeout=30)

One catch: environment variables (next section) can take priority over s.proxies. If your Session seems to ignore the proxy, pass proxies= on each call or turn off trust_env.

Environment variables and trust_env

requests also reads the standard proxy variables from your system: HTTP_PROXY, HTTPS_PROXY and NO_PROXY. That means you can set a proxy without touching the code:

# macOS / Linux
export HTTPS_PROXY="http://user:pass@proxy-host:port"

# Windows PowerShell
$env:HTTPS_PROXY = "http://user:pass@proxy-host:port"

It also means a variable left over from another project can send your traffic somewhere you did not intend. To make a script ignore them completely:

s = requests.Session()
s.trust_env = False   # ignore HTTP_PROXY / HTTPS_PROXY / NO_PROXY

Note that requests does not read the Windows or Mac system proxy settings you set in the control panel — see Windows proxy settings and Mac proxy settings for why.

SOCKS5 proxies

SOCKS support needs an extra package:

pip install "requests[socks]"

Then use socks5h:// as the scheme:

proxy = "socks5h://user:pass@proxy-host:port"
proxies = {"http": proxy, "https": proxy}

The h means the proxy looks up the website’s address (remote DNS). Without it, the lookup happens on your machine, which can send you to a server meant for your own country. More on that in HTTP vs SOCKS5 proxy.

Rotating and static proxies

With a rotating proxy, you do not manage a list of addresses — you use one gateway address and the provider changes the outgoing address for you. Your code stays exactly as in the basic setup. See what a rotating proxy is.

With several static proxies, you choose which one each request uses. A simple round-robin:

from itertools import cycle

pool = cycle([
    "http://user:pass@static-1:port",
    "http://user:pass@static-2:port",
])

for url in urls:
    p = next(pool)
    r = requests.get(url, proxies={"http": p, "https": p}, timeout=30)

Which to choose depends on the task — see rotating vs static proxy.

Timeouts and retries

Always set a timeout. Without one, a request through a slow or dead proxy can hang forever. You can give separate values for connecting and reading:

r = requests.get(url, proxies=proxies, timeout=(10, 30))  # 10s to connect, 30s to read

For occasional failures, retry a small number of times with a growing pause, rather than hammering the site:

from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

s = requests.Session()
retry = Retry(total=3, backoff_factor=2, status_forcelist=[502, 503, 504])
s.mount("http://", HTTPAdapter(max_retries=retry))
s.mount("https://", HTTPAdapter(max_retries=retry))
s.proxies.update(proxies)

Do not retry blindly on every error. If a site responds with a refusal or a “too many requests” status, slow down and check its stated rules rather than sending more.

Checking the proxy is really used

direct = requests.get("https://api.ipify.org", timeout=30).text
via    = requests.get("https://api.ipify.org", proxies=proxies, timeout=30).text
print("direct:", direct, "| via proxy:", via)

The two addresses should differ. Then check a page you actually care about — currency, language, delivery country — to confirm the location. For a fuller method, see how to test proxy quality.

Common errors and fixes

Error Usual cause Fix
ProxyError: Cannot connect to proxy Wrong host or port, or the proxy is unreachable Check the details; test with curl -x to rule out your code
407 Proxy Authentication Required Wrong login, unencoded special characters, or your IP not allow-listed Re-check credentials; use quote(); confirm your public IP
SSLError / WRONG_VERSION_NUMBER Proxy written as https:// when it speaks plain HTTP Use http:// for the proxy address
Missing dependencies for SOCKS support SOCKS package not installed pip install "requests[socks]"
ConnectTimeout / ReadTimeout Slow proxy, far location, or a heavy page Raise timeouts slightly; pick a closer location; check the target
Works, but shows your own address Only the "http" key set, or NO_PROXY matches the site Set both keys; check environment variables

One thing not to do: switching off certificate checks with verify=False to make an SSL error go away. That removes the protection HTTPS gives you. Fix the proxy address instead.

Using YiluProxy with requests

All five YiluProxy lines give you a host, port and login that drop straight into the code above, over HTTP or SOCKS5: rotating residential, static residential, rotating datacenter, static datacenter and mobile. For volume work on sources that treat every visitor alike, the datacenter lines are usually the cheaper starting point. To check a proxy before touching your code, a one-line test with curl and a proxy is the quickest route.

A proxy changes where requests come from; it does not change any website’s rules. Use it for publicly visible pages and your own assets, at a reasonable pace.

FAQ

Q1. How do I set a proxy for all requests in a script?

A. Create a requests.Session() and set s.proxies once, or set the HTTPS_PROXY environment variable before running the script.

Q2. Should the https key use an https:// proxy?

A. Usually no. The key is the website’s scheme; the value is how to reach the proxy, which is normally http:// even for HTTPS websites.

Q3. Does requests use my Windows or Mac proxy settings?

A. No. It reads environment variables and whatever you pass in code, not the operating system’s proxy panel.

Q4. How do I use a different proxy for each request?

A. Pass a different proxies dictionary on each call, for example cycling through a list of static addresses, or use a rotating gateway and let the provider switch addresses for you.


Test first, then buy

Paste a trial proxy into the basic example and run it against your own target pages.

Start a free trial

(Code examples use the requests library’s current behaviour. They contain no pool-size, success-rate or performance figures, as those vary by provider, target and time. Written November 2026.)

Scroll to Top