Skip to content
PhantomProxies

Integration

How to Use Proxies With Python

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.

Last updated September 7, 20268 min read

HTTP and HTTPS destinations

Install requests, copy the HTTP(S) endpoint from the dashboard, and provide the same proxy URL for both destination schemes.

python
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.

python
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.

python
try:
    response = requests.get(url, proxies=proxies, timeout=30)
    response.raise_for_status()
except requests.exceptions.ProxyError as error:
    print(f"Proxy connection failed: {error}")
except requests.exceptions.Timeout:
    print("Request timed out")