PYTHON 3 / QUICKSTART
Bring a proxy connection into Python.
Use Requests with environment variables, encoded credentials and an explicit timeout.
Prerequisites
Use Python 3 and an isolated virtual environment. The example uses the Requests package with an HTTP proxy endpoint.
Configuration guide. Purchasing and live connections are not open. Examples require actually provisioned credentials and do not create resources.
Install Requests in your virtual environment. Set the proxy environment variables in the process that runs your script. The example URL-encodes the username and password for special characters.
python -m pip install requestsEnvironment variables
Keep these values in your server process environment. Do not put proxy passwords or keys in browser code, public repositories or screenshots.
- PROXY_HOST
- Assigned proxy hostname, without a protocol prefix
- PROXY_PORT
- The port assigned for the chosen protocol
- PROXY_USERNAME
- Proxy username, separate from your website email
- PROXY_PASSWORD
- Proxy password, separate from the account password or API key
Make a request
The example uses an HTTP proxy to reach an HTTPS target. A SOCKS5 endpoint requires a compatible client and its assigned port; an HTTP proxy URL is not interchangeable with SOCKS5.
import os
from urllib.parse import quote
import requests
user = quote(os.environ["PROXY_USERNAME"], safe="")
secret = quote(os.environ["PROXY_PASSWORD"], safe="")
host = os.environ["PROXY_HOST"]
port = os.environ["PROXY_PORT"]
proxy = f"http://{user}:{secret}@{host}:{port}"
response = requests.get("https://httpbin.org/ip",
proxies={"http": proxy, "https": proxy}, timeout=30)
response.raise_for_status()
print(response.text)Check the result
Run the script once and check the returned exit IP. HTTP errors raise an exception, and requests have a 30-second timeout. Add application-specific error handling before scheduling larger tasks.
Troubleshooting
| Symptom | What to check |
|---|---|
| 407 | Check the proxy username, password and resource status. Avoid repeated automatic retries. |
| Timeout / connection error | Check host, port, firewall and protocol. Diagnose with a single request first. |
| 403 / 429 | Distinguish target responses from proxy authentication. Respect access rules, lower the rate and stop disallowed requests. |
| Unexpected exit | Check whether the plan rotates, the session has expired or the region parameter is supported by the resource. |