{"id":1461,"date":"2026-10-12T11:17:00","date_gmt":"2026-10-12T03:17:00","guid":{"rendered":"\/blog\/?p=1461"},"modified":"2026-10-06T08:39:00","modified_gmt":"2026-10-06T00:39:00","slug":"python-requests-proxy","status":"publish","type":"post","link":"\/blog\/python-requests-proxy","title":{"rendered":"How to Use a Proxy with Python Requests (Production-Ready Example)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To use a proxy with Python Requests, pass a proxy URL for both HTTP and HTTPS traffic. For an authenticated ColaProxy endpoint, take the gateway host, port, username, and password from your dashboard, store them outside the code, and build the proxy URL at runtime.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The example below is designed to be copied into a real project. It URL-encodes credentials, sets explicit timeouts, verifies the exit IP, and reports the most common connection failures without printing secrets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python 3.9 or newer<\/li>\n\n\n\n<li>The <code>requests<\/code> package<\/li>\n\n\n\n<li>An active proxy endpoint and credentials from the ColaProxy dashboard<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>python -m pip install requests<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">1. Store the proxy credentials outside your code<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Set these values in your terminal or deployment secret manager. Replace the placeholders with the gateway information displayed in your ColaProxy dashboard.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export PROXY_HOST=\"GATEWAY_HOST\"\nexport PROXY_PORT=\"PORT\"\nexport PROXY_USERNAME=\"YOUR_USERNAME\"\nexport PROXY_PASSWORD=\"YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Do not commit live proxy credentials to Git. Environment variables are sufficient for a local example; production systems should use the secret-management service provided by their hosting platform.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Create a reusable Requests session<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nfrom urllib.parse import quote\n\nimport requests\nfrom requests.exceptions import ProxyError, SSLError, Timeout, RequestException\n\n\ndef required_env(name: str) -&gt; str:\n    value = os.getenv(name)\n    if not value:\n        raise RuntimeError(f\"Missing required environment variable: {name}\")\n    return value\n\n\nhost = required_env(\"PROXY_HOST\")\nport = required_env(\"PROXY_PORT\")\nusername = quote(required_env(\"PROXY_USERNAME\"), safe=\"\")\npassword = quote(required_env(\"PROXY_PASSWORD\"), safe=\"\")\n\nproxy_url = f\"http:\/\/{username}:{password}@{host}:{port}\"\n\nsession = requests.Session()\nsession.proxies.update({\n    \"http\": proxy_url,\n    \"https\": proxy_url,\n})\nsession.headers.update({\n    \"User-Agent\": \"ColaProxy-Python-Example\/1.0\"\n})\n\ntry:\n    response = session.get(\"https:\/\/httpbin.org\/ip\", timeout=(10, 30))\n    response.raise_for_status()\n    print(\"Proxy connection works. Exit IP response:\", response.json())\nexcept ProxyError as exc:\n    print(\"Proxy connection or authentication failed:\", exc)\nexcept Timeout:\n    print(\"The proxy or destination did not respond before the timeout.\")\nexcept SSLError as exc:\n    print(\"TLS certificate validation failed:\", exc)\nexcept RequestException as exc:\n    print(\"The request failed:\", exc)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Although the destination uses HTTPS, the proxy URL commonly begins with <code>http:\/\/<\/code>. Requests connects to the HTTP proxy and establishes a tunnel for the HTTPS destination. Use the protocol and endpoint format shown in your provider dashboard rather than guessing from the destination URL.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Verify the exit IP before sending workload traffic<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The first request should go to an IP-check endpoint, not directly into a crawler or production job. Confirm that the returned IP differs from your local public IP and, when you selected a location, that its geolocation matches the intended market.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For automated systems, make this a startup health check. Stop the job if authentication fails or the exit IP is unexpected. This prevents a configuration error from sending traffic directly from your server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Requests Proxy: Per-Request Configuration vs Session<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For one request, pass the dictionary directly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>response = requests.get(\n    \"https:\/\/httpbin.org\/ip\",\n    proxies={\"http\": proxy_url, \"https\": proxy_url},\n    timeout=(10, 30),\n)\nresponse.raise_for_status()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use a <code>Session<\/code> when several requests share proxy settings, headers, or cookies. A session also reuses connections, which avoids unnecessary connection setup. Session reuse does not by itself guarantee a fixed exit IP: rotation behavior depends on the proxy product and session configuration selected in the <a href=\"https:\/\/colaproxy.com\/dynamic-residential-proxies\" target=\"_blank\" rel=\"noopener\">ColaProxy <\/a>dashboard.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Important: environment proxy variables can override your session<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Requests reads standard environment variables such as <code>HTTP_PROXY<\/code>, <code>HTTPS_PROXY<\/code>, and <code>NO_PROXY<\/code>. Its official documentation warns that values assigned to <code>session.proxies<\/code> may be overwritten by environment proxy settings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your code appears to use the wrong proxy, inspect the environment first:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>env | grep -i proxy<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For a process that must ignore all environment-derived proxy and certificate settings, explicitly disable that behavior:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>session = requests.Session()\nsession.trust_env = False\nsession.proxies.update({\n    \"http\": proxy_url,\n    \"https\": proxy_url,\n})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Do not assume that passing an empty <code>proxies={}<\/code> always bypasses a proxy configured in the environment. Use <code>NO_PROXY<\/code> for selected hosts, or a session with <code>trust_env = False<\/code> when direct routing is intentional.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Symptom<\/th><th>Likely cause<\/th><th>What to check<\/th><\/tr><\/thead><tbody><tr><td><code>407 Proxy Authentication Required<\/code><\/td><td>Missing or invalid credentials<\/td><td>Copy the username and password again, URL-encode special characters, and confirm that the account or plan is active.<\/td><\/tr><tr><td>Connection timeout<\/td><td>Wrong host or port, unavailable endpoint, blocked destination, or slow network<\/td><td>Verify the dashboard endpoint, test the IP-check URL, and keep separate connect and read timeouts.<\/td><\/tr><tr><td><code>ProxyError<\/code> or connection refused<\/td><td>The client cannot reach the proxy gateway<\/td><td>Check DNS, firewall rules, protocol, gateway hostname, and port.<\/td><\/tr><tr><td>SSL certificate error<\/td><td>Local CA configuration, TLS interception, or an incomplete certificate chain<\/td><td>Update the CA bundle and inspect the network path. Do not use <code>verify=False<\/code> as a production fix.<\/td><\/tr><tr><td>Unexpected exit IP<\/td><td>Environment variables overrode the session, or rotation\/session settings differ from expectations<\/td><td>Inspect proxy environment variables, check <code>trust_env<\/code>, and confirm the product&#8217;s rotation settings.<\/td><\/tr><tr><td>Credentials fail only with special characters<\/td><td>The proxy URL is malformed<\/td><td>Encode the username and password with <code>urllib.parse.quote(..., safe=\"\")<\/code>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Rotation and sticky sessions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ColaProxy&#8217;s public product information describes both per-request rotation and sticky-session options for dynamic residential proxies. Choose based on the task:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Rotation:<\/strong> useful when independent requests should use different exit IPs.<\/li>\n\n\n\n<li><strong>Sticky session:<\/strong> useful when a sequence of requests needs continuity, such as maintaining a login or a multi-step flow.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The exact username parameters or session controls may vary by product. Copy the generated credentials from the ColaProxy dashboard instead of constructing location or session syntax from an unrelated example.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Responsible use<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Only access data you are authorized to use. Respect applicable laws, website terms, robots directives where relevant, authentication boundaries, and reasonable request rates. A proxy changes network routing; it does not grant permission to access restricted content.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Why do both dictionary values use the same proxy URL?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The dictionary keys describe the destination scheme. Many authenticated proxy gateways accept HTTP proxy connections and tunnel HTTPS destinations, so both keys can point to the same endpoint. Follow the endpoint format supplied in your dashboard.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does Requests support <a href=\"\/blog\/socks5-proxies-essential-2026-success\" data-type=\"post\" data-id=\"479\">SOCKS5 proxies<\/a>?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, after installing the SOCKS extra with <code>python -m pip install \"requests[socks]\"<\/code>. Use a SOCKS endpoint only when your proxy product provides one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How should I keep the same IP across several requests?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Python <code>Session<\/code> reuses client settings, but proxy IP persistence is controlled by the provider&#8217;s sticky-session configuration. Generate or copy the appropriate session credentials from the ColaProxy dashboard.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Next step<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run the IP verification example with the endpoint from your ColaProxy dashboard. Once the exit IP and location are correct, move the same session into your application and add workload-specific retry and rate-limit handling.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Configure an authenticated proxy in Python Requests, keep credentials out of source code, verify the exit IP, and diagnose common 407, timeout, and SSL errors.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1461","post","type-post","status-publish","format-standard","hentry","category-proxy"],"_links":{"self":[{"href":"\/blog\/wp-json\/wp\/v2\/posts\/1461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/comments?post=1461"}],"version-history":[{"count":2,"href":"\/blog\/wp-json\/wp\/v2\/posts\/1461\/revisions"}],"predecessor-version":[{"id":2036,"href":"\/blog\/wp-json\/wp\/v2\/posts\/1461\/revisions\/2036"}],"wp:attachment":[{"href":"\/blog\/wp-json\/wp\/v2\/media?parent=1461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/categories?post=1461"},{"taxonomy":"post_tag","embeddable":true,"href":"\/blog\/wp-json\/wp\/v2\/tags?post=1461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}