Integration
Python Requests Proxies: HTTP and SOCKS5 Setup
Python's requests library accepts an authenticated proxy URL for both HTTP and HTTPS destinations. Use the HTTP(S) endpoint from your dashboard, set a timeout, and keep credentials in environment variables rather than source code.
HTTP and HTTPS destinations
Set the http and https keys in the requests proxies dictionary to the dashboard's authenticated HTTP proxy URL. Those keys describe the destination scheme; an HTTPS destination can still use an http:// proxy gateway. Install requests and keep the URL in an environment variable.
import os
import requests
proxy = os.environ["PROXY_URL"]
proxies = {"http": proxy, "https": proxy}
response = requests.get(
"https://ipinfo.io/json",
proxies=proxies,
timeout=30,
)
response.raise_for_status()
print(response.json())SOCKS5 with remote DNS
Install the SOCKS extra with pip install requests[socks]. Use the SOCKS5 endpoint shown in the dashboard. The socks5h scheme sends DNS resolution through the proxy, which is normally preferable for location testing.
import os
import requests
proxy = os.environ["SOCKS5_PROXY_URL"].replace("socks5://", "socks5h://", 1)
response = requests.get(
"https://ipinfo.io/json",
proxies={"http": proxy, "https": proxy},
timeout=30,
)
response.raise_for_status()
print(response.json())Rotation and sticky sessions
Generate the configured password in the PhantomProxies dashboard instead of hand-editing it. A rotating password has no session identifier. Sticky output includes a unique eight-character session ID and lifetime.
- Reuse one generated sticky URL to request continuity.
- Generate a new line for an independent sticky session.
- Expect a route to change early if its residential device goes offline.
Handle failures explicitly
Catch timeouts and proxy connection errors separately from HTTP errors returned by the destination. Retry conservatively with backoff; unlimited retries can consume bandwidth and overload the target.
try:
response = requests.get(url, proxies=proxies, timeout=30)
response.raise_for_status()
except requests.exceptions.ProxyError:
print("Proxy connection failed; inspect sanitized diagnostics")
except requests.exceptions.Timeout:
print("Request timed out")