By GlobalProxyIP Editorial Team
A Requests proxy configuration can look valid while the request uses an unintended route or fails before reaching the target. Start by separating four inputs: the target URL scheme, the proxy transport scheme, proxy credentials, and inherited environment settings. Then test DNS resolution as a separate variable.
Mistake 1: matching the proxy URI to the target scheme
The dictionary key https selects HTTPS destinations. Its value can be an http:// proxy URI when that endpoint supports CONNECT. Changing the value to https:// instructs the client to use a different transport to the proxy; it does not simply turn on target HTTPS.
For an endpoint that supports both destination types:
proxies = {"http": proxy_uri, "https": proxy_uri}
Select the value from the endpoint’s actual protocol support. An IP retrieval API URL is not a proxy URI; retrieve and validate the endpoint first if that is how the service delivers proxies.
Mistake 2: sending proxy credentials to the target
Requests’ ordinary auth= argument concerns target authentication. For a Basic-authenticated HTTP proxy, the documented proxy URI format is http://user:password@host:port. Encode the username and password separately. Encoding the entire URI would also encode its structure; encoding an already encoded password a second time would change the credential.
Do not log the complete URI or raw connection exception. They may contain credentials. For automated jobs, obtain secrets from a controlled store and restrict diagnostic access.
Mistake 3: assuming Session settings always override the environment
Requests documents that environmental proxies may overwrite session.proxies. For an explicit per-call configuration, pass proxies= with the relevant destination schemes. For an isolated diagnostic session, also set session.trust_env=False.
That switch affects more than proxy routing, including environment CA settings and default credential behavior. Configure a trusted enterprise CA explicitly if the deployment requires one. Passing proxies={} alone is not a reliable instruction to disable all inherited proxy behavior.
Mistake 4: treating socks5 and socks5h as interchangeable
Install the optional dependency in the same interpreter used to execute the script:
python -m pip install 'requests[socks]'
| URI scheme | Target name resolution | What remains local |
|---|---|---|
socks5:// |
Client | Proxy hostname and target hostname lookup |
socks5h:// |
Proxy | Proxy hostname lookup, if the endpoint is named |
Switch only this variable when diagnosing target DNS failures. A proxy-side DNS problem is not resolved merely by changing the client resolver. Requests still sends HTTP requests; SOCKS support does not turn it into a general UDP client.
Run a controlled configuration check
Use Python 3 with Requests 2.x. Set PROXY_HOST, PROXY_PORT, PROXY_SCHEME, and TARGET_URL. The target must return a JSON object with a string origin field; adapt that check to your own echo contract. Enter raw credentials at the prompts, or leave the username empty only for an endpoint already authorized without a password.
import json
import os
import time
from getpass import getpass
from urllib.parse import quote
import requests
scheme = os.environ.get('PROXY_SCHEME', 'http')
if scheme not in {'http', 'https', 'socks5', 'socks5h'}:
raise SystemExit('Unsupported proxy scheme')
host = os.environ['PROXY_HOST']
port = int(os.environ['PROXY_PORT'])
if not 1 <= port <= 65535 or any(c in host for c in '/@?#'):
raise SystemExit('Invalid endpoint')
user = input('Proxy username (empty for an authorized no-password endpoint): ')
credential = ''
if user:
credential = f'{quote(user, safe="")}:{quote(getpass("Proxy password: "), safe="")}@'
proxy = f'{scheme}://{credential}{host}:{port}'
started = time.monotonic()
with requests.Session() as session:
session.trust_env = False
if os.environ.get('TRUSTED_CA_FILE'):
session.verify = os.environ['TRUSTED_CA_FILE']
try:
with session.get(os.environ['TARGET_URL'],
proxies={'http': proxy, 'https': proxy},
timeout=(5, 15), allow_redirects=False) as response:
response.raise_for_status()
if response.status_code != 200:
raise ValueError('Unexpected status; inspect redirects separately')
data = response.json()
if not isinstance(data, dict) or not isinstance(data.get('origin'), str):
raise ValueError('Expected a JSON object with an origin string')
print(json.dumps({'status': response.status_code, 'origin': data['origin'],
'elapsed_s': round(time.monotonic()-started, 3)}, ensure_ascii=False))
except (requests.RequestException, ValueError) as error:
print(json.dumps({'error_type': type(error).__name__,
'elapsed_s': round(time.monotonic()-started, 3)}))
raise SystemExit(1) from None
Keep all other settings fixed while comparing HTTP and SOCKS endpoints. They may use different ports or accounts, so record each complete configuration as a separate case. The observed duration is not a throughput benchmark, and the connect/read timeout pair is not a wall-clock deadline.
Frequently asked questions
Why does HTTPS authentication failure appear as ProxyError?
A proxy can reject CONNECT before the client receives a normal target response. Inspect the redacted connection stage rather than expecting every failure to appear as response.status_code == 407.
Does trust_env=False disable TLS verification?
No. Certificate verification remains enabled. It does change environmental CA loading, so use an explicit approved CA path when needed.
Does a Session preserve the proxy exit?
A Session retains client-side state and reuses suitable connections. Exit persistence remains a separate property of the proxy service and selected session settings.
References
Related concepts
For protocol selection, compare HTTP proxy forwarding and SOCKS5 negotiation; for HTTPS through an HTTP endpoint, review the separate CONNECT tunnel stage.