
A proxy in Python requests is a gateway URL stored on the Session. Each call goes to that gateway first, and the site sees the exit IP instead of your machine. The dict keys name the scheme of the URL you are calling, so http and https both need the gateway string. An empty https key sends that call out from your own address, and the echo service prints your office IP.
What the proxies dict controls
The proxies dict is the map from a target scheme to a gateway URL. The key is http or https on the page you are opening, not the kind of proxy. A SOCKS gateway still belongs under the https key when the target starts with https://.
That split is why a working http entry can still leak the real address. requests looks up the key from the target URL. A missing https entry means the library opens the store, the search page, or the echo service directly, and the gigabytes on the gateway never move.
Add SOCKS to requests
SOCKS support in requests comes from PySocks, and the extra install pulls that package in.
pip install "requests[socks]" A gateway URL that starts with socks5h:// asks the gateway to resolve the hostname. The lookup sits next to the exit IP the site will see, so the name and the address belong to the same hop. An HTTP gateway uses a URL that starts with http:// instead. The dashboard prints one of those two forms. Copy the form it prints.
Put the gateway on a Session
A Session is the object that keeps the gateway, the headers, and the cookie jar together. Later calls in the same run reuse that jar, so a second page still carries the first response.
import os from urllib.parse import quote import requests user = quote(os.environ["PROXY_USER"], safe="") password = quote(os.environ["PROXY_PASS"], safe="") host = os.environ["PROXY_HOST"] # host:port from the dashboard proxy = f"socks5h://{user}:{password}@{host}" session = requests.Session() session.trust_env = False session.proxies = {"http": proxy, "https": proxy} exit_ip = session.get("https://api.ipify.org", timeout=30).text print(exit_ip) quote keeps a password that contains @ or : inside the userinfo. A raw @ splits the URL early, and the gateway answers as if the login were wrong. trust_env = False stops a leftover HTTPS_PROXY in the shell from replacing the gateway you just set. The timeout ends the wait when the gateway does not answer, instead of leaving the process hung.
You point the same Session at the real host only after the printed address matches the country you picked. The gigabytes start on that second request.
Match the username to the job
The username is the field most gateways use for the country and the session mode. A rotating user gives the next request a new exit IP from the pool. A sticky user holds one exit IP for a single job, then lets it go.
A long product list can use the rotating user. Each page leaves from another household, and one connection does not absorb the whole list. A checkout uses the sticky user. The item page and the payment page then share a visitor, and the cart stays attached to that address. IPOasis sticky sessions hold that address for up to 120 minutes. When the window ends, the next request can leave from a different household IP. The dashboard builds the username. That string goes in PROXY_USER.
Read a refusal before you swap the exit
A 200 from the echo service means the gateway accepted the login and the exit country is the one you set. A 403 from the store is a separate reply. The exit IP can be a household address and the edge can still refuse the request. How to read a Cloudflare 403 keeps the status and the headers in view before the gateway string changes.
Where the gateway string comes from
The host, the port, the username, and the password come from the provider dashboard. IPOasis lists dynamic residential proxies from $0.78 per GB, with HTTP and SOCKS5 on the same balance. Unused gigabytes stay on the balance, so a short exit check does not burn a monthly plan.
FAQ
How do you set a proxy in Python requests?
You set a proxy in Python requests by putting a gateway URL on Session.proxies for both http and https. The Session then sends each call to that gateway, and the site sees the exit IP. One requests.get with a proxies argument works for a single call, and it drops the cookie jar when the next call starts.
What is socks5h?
socks5h is a SOCKS5 URL whose hostname is resolved at the gateway. The DNS lookup and the exit IP sit on the same hop, so the site and the resolver see one network. The extra package is requests[socks]. An HTTP gateway uses an http:// URL instead, and the dashboard shows which form you have.
Why does the Session print my real IP?
The Session prints your real IP when the https key is missing, or when HTTPS_PROXY in the shell replaced the gateway. requests picks the dict entry from the target scheme, so an https:// echo skips an http-only map. trust_env = False keeps the shell value out, and both keys need the same gateway string.
What is a sticky session?
A sticky session keeps one exit IP for a single job, then releases it. A checkout stays one visitor from the item page to the payment page, so the cart does not reset. A rotating user is the other setting, and it gives the next URL a new household address. The dashboard username is where that choice is stored.
Which environment variables does the example read?
The example reads PROXY_USER, PROXY_PASS, and PROXY_HOST from the environment. The host value already includes the port the dashboard printed. Keeping the password out of the source file means a pasted snippet does not carry a live login, and quote still encodes @ and : before the URL is built.
