DashboardDashboardLog inTry Free for 2 Hours

How to use a proxy with Python requests and httpx

Python's requests library takes a proxy through its proxies argument, and httpx takes one through proxy. This guide sets up both with an authenticated mobile proxy over HTTP or SOCKS5, then shows how to keep a long script steady when the proxy's IP changes on its timer.

Replace HOST, PORT, USERNAME and PASSWORD in the examples with the values from your dashboard. Plans and prices are on pricing.

AtlasProxies Team · Updated · 5 min read

  • 1Map Both Schemesrequests picks a proxy by the target URL's scheme, so point 'http' and 'https' at the same proxy URL.
  • 2SOCKS5 Needs an ExtraInstall requests[socks] or httpx[socks] before you use a SOCKS5 proxy URL.
  • 3Retry Idempotent Calls OnlyMount a urllib3 Retry on the Session for reads, and leave writes to your own logic.
On This Page
  1. Build the proxy URL
  2. Set a proxy in requests
  3. SOCKS5 with requests[socks]
  4. The same setup in httpx
  5. Check the exit IP from Python
  6. Retries for scripts that outlive the rotation timer
  7. Several proxies in one script

Build the proxy URL

Both libraries want the proxy as one URL made of scheme, login, host and port. In My Proxies, the "User:Pass@IP:Port" copy format gives you everything after the scheme, so the HTTP version reads http://USERNAME:PASSWORD@HOST:PORT. Keep it in an environment variable rather than in the source file, and read it with os.environ['PROXY_URL'] so the login never lands in version control.

If the password contains @, : or another reserved character, percent-encode it before building the URL. urllib.parse.quote(PASSWORD, safe='') from the standard library does that in one call.

Set a proxy in requests

The proxies argument is a dictionary keyed by the scheme of the URL you are requesting. Most code needs both keys pointing at the same proxy:

proxies = {'http': PROXY_URL, 'https': PROXY_URL}
requests.get('https://api.ipify.org', proxies=proxies, timeout=30)

The value for 'https' still starts with http://, because it describes how to reach the proxy, not the site.

For a script that makes many calls, a Session reuses connections and keeps cookies between requests. Call this once after creating it:

session.proxies.update(proxies)

The requests documentation notes that proxy environment variables can override what you set on session.proxies, so pass proxies=proxies on each call when the environment might define its own.

When no proxies argument is given at all, requests falls back to HTTP_PROXY, HTTPS_PROXY and ALL_PROXY from the environment. That is a quick way to run an unmodified script through the proxy. Set the variable before starting it:

export HTTPS_PROXY=http://USERNAME:PASSWORD@HOST:PORT

SOCKS5 with requests[socks]

SOCKS support in requests is optional and comes from an extra:

python -m pip install 'requests[socks]'

After that, change the scheme in the proxy URL to socks5h://USERNAME:PASSWORD@HOST:PORT and keep the same proxies dictionary.

Pick socks5h over socks5. The requests docs explain that socks5 resolves hostnames on your machine, while socks5h hands the lookup to the proxy, so DNS goes out over the mobile connection like the rest of the traffic. Switch the proxy to SOCKS5 in the dashboard first and copy the proxy line again. SOCKS5 mobile proxies covers what the option includes.

The same setup in httpx

httpx takes a single proxy URL through the proxy parameter, on a client or on top-level calls:

with httpx.Client(proxy=PROXY_URL) as client:
    client.get('https://api.ipify.org')

The login goes inside the URL in the same way, and httpx.AsyncClient accepts the same argument for asyncio code.

For SOCKS5, install the socks extra:

pip install 'httpx[socks]'

Then pass a SOCKS5 URL. The httpx docs show the form socks5://user:pass@host:port.

To route plain-HTTP and HTTPS targets through different proxies, httpx uses mounts, a dictionary that maps "http://" and "https://" to httpx.HTTPTransport(proxy=...). With one proxy for everything, the proxy argument alone is enough.

Check the exit IP from Python

A short check catches most setup mistakes. Request https://api.ipify.org with and without the proxy and print both response bodies. They should differ, and the proxied one should match Current Public IP in the proxy's IP Rotation panel. If the proxied call raises requests.exceptions.ProxyError, read the message: a 407 means the login was refused, and 407 Proxy Authentication Required lists the usual fixes.

Set a timeout on every call. requests waits indefinitely by default, so a call caught by a rotation could hang instead of failing and being retried.

Retries for scripts that outlive the rotation timer

The proxy gets a new IP whenever the timer you set fires, anywhere from every 5 to every 120 minutes, and no API call or link rotates it on demand. Your proxy URL stays valid across the change. Connections pooled by a Session on the old IP can be dropped, and the next call on one of them may fail with requests.exceptions.ConnectionError, or httpx.ReadError or httpx.RemoteProtocolError in httpx, often with a message about a connection reset by peer.

In requests, mount an HTTPAdapter with a urllib3 Retry on the Session:

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

retries = Retry(total=3, backoff_factor=1)
session.mount('https://', HTTPAdapter(max_retries=retries))

By default urllib3 retries only methods it treats as idempotent, such as GET, HEAD, PUT and DELETE, and leaves POST alone. Keep it that way unless the endpoint accepts duplicates.

httpx's own retries option, as in httpx.HTTPTransport(retries=3), repeats only failed connection attempts (ConnectError and ConnectTimeout). A reset on a request that was already sent needs your own loop or a library such as tenacity, which the httpx docs suggest.

Shape each unit of work so a repeat is harmless. Fetch by ID, write results keyed by URL and record progress after each item, so a restart resumes at the next item. A longer interval keeps more of each run on one IP, and rotating mobile proxies explains how to choose it.

Several proxies in one script

Give each proxy its own Session or Client instead of swapping proxies on a shared one. Each object keeps its own connection pool and cookies, so work assigned to one proxy stays on that proxy. A dictionary that maps a label, such as the country, to a Session is enough to manage a handful.

Plan data use per proxy. Eco and Pro have unlimited bandwidth with 15 GB/day of fair use, and a proxy that reaches 15 GB in a day stops until the limit resets the next day. Ultra is truly unlimited. For collecting public data on a schedule, see web scraping proxies.

Good to Know. Collect only what each website's terms allow, and follow our Acceptable Use Policy.

Frequently Asked Questions

Why do I need both 'http' and 'https' keys in proxies?

requests chooses the proxy by the scheme of the URL you request. With only 'http' set, calls to https:// sites skip the proxy.

Does requests support SOCKS5 proxies?

Yes, once the requests[socks] extra is installed. Use a socks5h:// URL so the proxy resolves hostnames.

Should the 'https' value start with http:// or https://?

With http://. The value says how to reach the proxy, and an HTTP proxy is reached over plain HTTP even when the site itself uses HTTPS.

How do I use the same proxy in httpx?

Pass the proxy URL to the client, as in httpx.Client(proxy=PROXY_URL). For SOCKS5, install httpx[socks] first.

Can my Python script request a new IP?

No. The IP changes only on the rotation timer you set in the dashboard, from every 5 to every 120 minutes. Build retries around it rather than trying to trigger it.

Your first mobile proxy is two minutes away.

Sign up, pick a country, and your login arrives in seconds.