requests
Use a static IP address from Python using requests.
The requests library supports proxy configuration through the proxies parameter. Embed the token in the proxy URL as basic auth credentials.
Prerequisites
- A Rayley account with a Proxy Token (
rpt_*). Create one in the dashboard.
Installation
pip install requestsQuick start
Pass a proxies dict to route the request through your static IP address. Embed the token in the proxy URL as basic auth credentials:
import requests
proxy_url = "https://token:rpt_your_token_here@proxy.rayley.com"
response = requests.get(
"https://api.example.com/data",
proxies={"https": proxy_url},
timeout=30,
)
print(response.json())Session
Use a Session to reuse the proxy configuration across requests without passing proxies each time:
import requests
proxy_url = "https://token:rpt_your_token_here@proxy.rayley.com"
session = requests.Session()
session.proxies = {"https": proxy_url}
# These requests use your static IP Address
response = session.get("https://api.example.com/data", timeout=30)
print(response.json())
# Requests without the session go directly to the destination
response2 = requests.get("https://api.example.com/other-data", timeout=30)
print(response2.json())Full example
Below is a command-line script you can use to test your Proxy Token. It reads the token from the RAYLEY_TOKEN environment variable and takes an optional URL argument (defaulting to https://example.com) to make a request using your static IP address.
import os
import sys
import requests
proxy_token = os.environ.get("RAYLEY_TOKEN")
target_url = sys.argv[1] if len(sys.argv) > 1 else "https://example.com/"
if not proxy_token:
print("Set RAYLEY_TOKEN to your Proxy Token.", file=sys.stderr)
sys.exit(1)
proxy_url = f"https://token:{proxy_token}@proxy.rayley.com"
response = requests.get(
target_url,
proxies={"https": proxy_url},
timeout=30,
)
if response.status_code == 407:
print("Authentication failed. Check your RAYLEY_TOKEN.", file=sys.stderr)
sys.exit(1)
response.raise_for_status()
print(f"Status: {response.status_code}")
print(response.json())Run it:
RAYLEY_TOKEN=rpt_your_token python example.py https://example.com/