Summary: Start using residential proxies in Python with a minimal request, then add credential management, location and session checks, and error handling. This article demonstrates the connection process with Requests. Use the gateway, port, and complete username generated in the Gleam dashboard.
Proxy connection failures often come down to basic configuration: an incorrectly copied port, missing username parameters, unhandled special characters in a password, or a new session accidentally created during a retry.
Before adding concurrency and business-specific parsing, confirm that the connection, authentication, and exit IP behave as expected. This greatly narrows the scope of troubleshooting.
Step 1: Prepare the Complete Connection Details
Obtain the proxy gateway, port, complete username, and password from the dashboard. If you need a specific location or sticky session, configure it in the generator first, then copy the full result.
The gateway is the entry point through which the client connects to the proxy network. The exit IP is the address the target website sees. Do not assume they represent the same location based on their names or positions; verify the result through actual requests.
Install Requests:
python -m pip install requestsThe following example sets environment variables in PowerShell. Replace the placeholders with the details provided by the dashboard:
$env:PROXY_HOST = "YOUR_PROXY_HOST"
$env:PROXY_PORT = "YOUR_PROXY_PORT"
$env:PROXY_USERNAME = "YOUR_COMPLETE_USERNAME"
$env:PROXY_PASSWORD = "YOUR_PASSWORD"These variables are available to the current terminal and the Python processes it starts. Other shells and deployment methods require the corresponding environment configuration.
Step 2: Send a Minimal Request
Start with an IP lookup endpoint to confirm that a request can return through the proxy:
import os
from urllib.parse import quote
import requests
host = os.environ["PROXY_HOST"]
port = int(os.environ["PROXY_PORT"])
username = quote(os.environ["PROXY_USERNAME"], safe="")
password = quote(os.environ["PROXY_PASSWORD"], safe="")
proxy_url = f"http://{username}:{password}@{host}:{port}"
proxies = {"http": proxy_url, "https": proxy_url}
with requests.Session() as session:
session.trust_env = False
response = session.get(
"https://api.ipify.org?format=json",
proxies=proxies,
timeout=(10, 30),
)
response.raise_for_status()
print(response.json())quote() encodes special characters in the username and password so that characters such as @ and : do not interfere with parsing the proxy URL. The code explicitly sets the proxy and disables the Session's automatic use of environment settings to reduce interference from other local proxy configurations. Adjust this if your deployment relies on environment settings such as custom certificates.
A successful response returns the exit IP for that request. The endpoint only returns an address; it does not directly confirm the exit country. Follow up with an appropriate location lookup or target page check.
The http:// in the code specifies an HTTP proxy connection. When the target website uses HTTPS, Requests can establish a tunnel through the HTTP proxy, so the two protocol prefixes do not have to match. If the dashboard provides a different connection method, adjust the configuration accordingly.
Step 3: Verify Location and Session Behavior
After establishing the connection, check the actual business conditions.
Verify the exit location first, then open a real target page and inspect the site version, currency, language, and redirects. If the task includes searching, filtering, and pagination, test the whole sequence.
Gleam's location and session settings can be included in the complete proxy username. Copy the generator's output rather than assembling parameters from memory.
For a sticky session, use consistent session settings throughout one continuous task. If a new session identifier is generated for every request, the previous exit IP may not be retained even when the configured duration is unchanged.
When checking IP rotation, do not expect every request in a small sample to return a different IP. Observe the overall behavior in line with the product's rules. When checking sticky sessions, confirm that the configuration stays consistent throughout the task and account for session duration and resource availability.
Step 4: Retry Only When Appropriate
Temporary timeouts may justify limited retries, but authentication failures and most client errors call for checking the configuration first. The following example makes only GET requests and allows up to three attempts for timeouts, connection errors, and a small set of temporary server errors.
It also stops immediately on HTTP 429 to avoid disregarding the target service's rate limits. Production tasks should handle waiting and recovery according to the target's rules.
import time
import requests
RETRYABLE_STATUS = {500, 502, 503, 504}
def fetch(session, url, proxies, max_attempts=3):
if max_attempts < 1:
raise ValueError("max_attempts must be at least 1")
for attempt in range(1, max_attempts + 1):
try:
response = session.get(
url,
proxies=proxies,
timeout=(10, 30),
)
except (
requests.exceptions.SSLError,
requests.exceptions.ProxyError,
):
# Check certificates, proxy settings, or authentication first.
raise RuntimeError("Proxy or TLS connection error; check configuration") from None
except (
requests.exceptions.Timeout,
requests.exceptions.ConnectionError,
):
if attempt == max_attempts:
raise RuntimeError("Connection failed; maximum attempts reached") from None
else:
status = response.status_code
if status in RETRYABLE_STATUS:
response.close()
if attempt == max_attempts:
raise RuntimeError(f"Service temporarily unavailable, HTTP {status}")
elif status >= 400:
response.close()
raise RuntimeError(f"Request stopped, HTTP {status}; investigate the cause")
else:
return response
time.sleep(2 ** (attempt - 1))
# Run after the connection configuration in the previous example.
with requests.Session() as session:
session.trust_env = False
result = fetch(
session,
"https://api.ipify.org?format=json",
proxies,
)
try:
print(result.json())
finally:
result.close()timeout=(10, 30) sets the connection timeout and read timeout, respectively. The read timeout primarily limits the interval spent waiting for data; it does not impose a 30-second limit on the entire download. Nor can it alone determine whether the fault occurred at the proxy gateway or the target server.
This example provides a basic structure for limited retries. Production tasks also need to handle request frequency, overall execution limits, and error alerts according to the target website's rules. Do not apply this GET retry logic directly to payments, form submissions, or other requests that could cause duplicate actions.
Step 5: Record Information That Helps Troubleshooting
Record the target region, request time, response status, duration, attempt count, and internal task ID. A full proxy URL contains credentials and should not be written directly to logs. Target URLs with sensitive parameters should also be processed appropriately before logging.
If IP lookups work but business pages behave unexpectedly, investigate the target site's rules, client state, and returned content. A working proxy connection does not guarantee correct page parsing or business results.
Frequently Asked Questions
What does HTTP 407 usually mean?
It generally means proxy authentication is required or has failed. Check the complete username, password, and authentication method. If you use IP allowlisting, confirm that the current public IP is authorized. Some HTTPS tunnel authentication failures appear as proxy connection errors.
Why does the exit IP change during a retry after I configure a sticky session?
First check whether the same session identifier is still in use and whether the configured duration has expired. Resource interruptions can also affect exit IP continuity.
Can I copy this example directly for SOCKS5?
It needs adjustment. Using a SOCKS proxy with Requests requires the relevant dependencies and a matching proxy protocol and address. This example demonstrates an HTTP proxy connection.
Does the Python code automatically produce structured product data?
No. It only handles requests and connection checks. Field extraction, content validation, and data storage must be implemented separately.