Use an authenticated proxy with Python Requests
Set up Requests proxy routing with encoded credentials, explicit timeouts and TLS verification, then diagnose common connection failures.
On this page Quick answer
Quick answer
Configure both http and https proxy keys for the destination schemes you use. Supply the gateway URL, preserve TLS verification and set a timeout. The .invalid endpoints below are safe placeholders, not a working MiProxy connection.
Requests proxy setup is a small configuration task with several independent boundaries: destination schemes, gateway transport, credential encoding, environment configuration and timeouts. Make each one explicit before diagnosing a failure.
This guide starts with an isolated Session and one authorized request. It explains why the example makes those choices, what it does not prove, and which information to preserve when moving the configuration into a maintained application.
How this guide was researched
A configuration guide grounded in official Requests proxy documentation and common authenticated-setup questions. Code uses reserved placeholder hosts and environment-supplied credentials; no production-allocation outcome is implied.
What we evaluate- Explicit destination-scheme proxy mapping.
- Encoding credential components without changing URL structure.
- Environment inheritance and approved TLS trust.
- Interpreting timeout, authentication and response failures separately.
Prepare an isolated client
Install Requests in a virtual environment and record the Python and Requests versions with your test. Supply real connection values through your environment or deployment secret manager. Do not commit a credential-bearing proxy URL.
The example uses an HTTP gateway for an HTTPS destination. Confirm your allocation’s supported transport and port. This guide covers Requests proxy configuration; it does not imply that MiProxy has a public management API.
Encode credentials and set a timeout
Percent-encoding each credential component prevents special characters such as @ from changing the structure of the proxy URL. Keep the original values in your environment and encode them once.
This explicit configuration disables inherited environment settings with trust_env=False. That also disables environment-based CA bundle configuration; organizations that require a custom trust bundle should set session.verify to the approved bundle path.
import os
from urllib.parse import quote
import requests
username = quote(os.environ["PROXY_USERNAME"], safe="")
password = quote(os.environ["PROXY_PASSWORD"], safe="")
proxy_url = f"http://{username}:{password}@gateway.invalid:8080"
with requests.Session() as session:
session.trust_env = False
session.proxies = {"http": proxy_url, "https": proxy_url}
response = session.get(
"https://check.invalid/ip",
timeout=(10, 30),
)
response.raise_for_status()
print(response.text)Interpret the result carefully
Replace both .invalid hosts before using the template. The timeout tuple limits connection establishment and read inactivity; it is not a guaranteed total runtime for a batch. Start with one request and inspect the expected response body.
A Requests Session reuses connections and cookies. Provider-side sticky routing is a separate setting. For an independent task, choose that boundary deliberately rather than assuming a new Python object always produces a new residential exit.
Read the proxy dictionary from the destination outward
The http and https keys describe destination URL schemes. They do not by themselves state that the proxy gateway accepts encrypted transport. In the example, both schemes use the same documented HTTP gateway URL. Change that only when your allocation supports a different gateway transport.
Choose environment behavior deliberately
Requests can use environment proxy configuration. That can be convenient on a managed workstation, but it can also make an otherwise identical script route differently on another machine. The example sets trust_env=False to make its explicit settings easier to reason about. This also affects environment-based CA bundle handling; configure an approved trust path explicitly where needed.
Client reuse and provider routing are different
A Requests Session can reuse connections and cookies. A provider’s rotating or sticky routing mode is another layer. Do not assume that creating a Session guarantees one exit or that a new Session guarantees a different exit. Check the provider’s documented boundary and compare it with the pattern your client actually uses.
Encode components, then construct the URL
A username or password can contain characters with special meaning in a URL. Percent-encode each credential component before inserting it into the gateway URL, as the example does. Encoding the entire completed URL would also transform its separators; encoding a previously encoded password can change the intended value.
Validate configuration before making the request
Check locally that each required environment value is present and that the gateway host and port match the active allocation. Error messages should identify the missing configuration name without printing its value. Keep the original secret in the environment or deployment secret store rather than committing an encoded copy.
Log the event without the credential-bearing URL
For operational logs, store a task label, time, client version, gateway hostname, destination category and failure class. A full proxy URL can include the password. Redact that structure before sending an exception report to analytics or support systems.
Know what the timeout tuple promises
The timeout tuple separates connection establishment from read inactivity. It does not create a guaranteed wall-clock deadline for an entire batch, and a successful response still needs content validation. Build the surrounding job with its own task boundary and completion rule.
Where it helps
- An explicit Session exposes proxy mapping, trust and timeout choices in one place.
- Environment-supplied secrets keep the literal password out of the example source.
- Component encoding makes reserved credential characters less ambiguous.
What to account for
- Disabling environment inheritance requires deliberate handling of organization trust settings.
- A read timeout is not a total batch-runtime budget.
- Session reuse does not define the gateway’s exit-rotation policy.
Classify a failure before adding recovery
If the proxy reports HTTP 407, examine proxy authentication and the supported method. A connection timeout, DNS failure or certificate error belongs to a different layer. If the destination returns an error after the tunnel is established, inspect that response and the destination’s access requirements separately.
Define useful output
raise_for_status helps surface unsuccessful HTTP responses. It does not validate the required fields or prove that an HTML response is the intended page. Check your endpoint’s documented structure and record missing or unexpected fields as content failures.
Keep retries finite and interpretable
Choose specific transient failures that may be retried and keep an attempt count. Credential mistakes should trigger a configuration review, while destination rate limits need an appropriate pause or stop policy. Do not silently combine output from restarted sessions when continuity matters to the task.
Handle failures without exposing secrets
Treat proxy authentication, TLS errors and destination HTTP errors separately. An HTTPS tunnel authentication failure can surface as ProxyError rather than an ordinary response object. A timeout alone does not tell you the exit was blocked.
Catch requests.exceptions.RequestException at your application boundary and log a sanitized error category. Avoid printing the proxy URL or raw exception into shared logs because connection diagnostics can expose credentials. Stop or back off on access and rate-limit errors rather than adding unlimited retries.
Which approach should you choose?
Configure a single bounded Session explicitly, preserve TLS verification and validate the response body before expanding the workload.
- You need deterministic proxy routing
- Set the relevant scheme keys and decide deliberately whether environment proxy settings should participate.
- Your organization supplies a CA bundle
- Preserve the approved trust configuration explicitly when disabling environment inheritance.
- The first request fails
- Separate proxy authentication, network establishment, TLS and destination response handling before retrying.
Where MiProxy fits
Explore the connection workflow, available packages, and setup guides.
Frequently asked questions
Why set both proxy dictionary keys?
The keys select destination URL schemes. Setting both makes the intended routing explicit for HTTP and HTTPS requests.
Should I set verify=False to fix a proxy error?
No. Preserve certificate verification and fix the gateway, hostname or approved trust configuration. Disabling TLS verification does not repair proxy authentication.
Why encode the proxy username and password separately?
Reserved URL characters inside credentials can otherwise change the URL’s structure. Encode each credential component once before building the gateway URL; do not encode the entire completed URL or expose it in logs.
What does trust_env=False change?
It prevents the Session from inheriting environment configuration used by Requests, including proxy settings and environment-based CA bundle handling. Set the required routing and approved certificate trust explicitly when choosing this isolation.
Does a Requests Session guarantee a sticky proxy exit?
No. A Session manages client connection reuse and cookies; the gateway’s routing policy is separate. Verify the provider’s supported session controls and the behavior of the actual allocation.
Sources & review notes
Reviewed October 17, 2026. Technical definitions and provider statements are attributed below. Recommendations are MiProxy’s editorial analysis; current vendor terms should be checked before purchase.
- Requests: advanced usage and proxies ↗Checked 2026-10-16
Save & use this guide
The Markdown version includes the complete explanation, comparison, FAQs and source links. Read it offline or use it as reference material in your own research.
Share this article
MiProxy editorial
We write practical proxy explanations, setup guides and buying comparisons. Our product status, research method and source limitations are stated alongside the advice.
About our editorial approach ↗

