Python requests ProxyError and SSL errors behind a proxy
What you see
requests.exceptions.ProxyErrorMax retries exceeded with urlCaused by ProxyError('Unable to connect to proxy'Tunnel connection failed: 407 Proxy Authentication Required
More error strings (17)
ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable'))ProxyError('Unable to connect to proxy', NewConnectionError(Failed to establish a new connection: [Errno 111] Connection refusedProxyError('Cannot connect to proxy.'Unable to connect to proxy. Your proxy appears to only use HTTP and not HTTPS, try changing your proxy URL to be HTTP.[SSL: WRONG_VERSION_NUMBER] wrong version numberrequests.exceptions.InvalidSchema: Missing dependencies for SOCKS support.requests.exceptions.SSLErrorCaused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failedcertificate verify failed: self-signed certificate in certificate chaincertificate verify failed: unable to get local issuer certificatecertificate verify failed: Basic Constraints of CA cert not marked criticalhttpx.ProxyError: 407 Proxy Authentication Requiredhttpx.ProxyError: _ssl.c:1091: The handshake operation timed outaiohttp.client_exceptions.ClientHttpProxyError: 407, message='Proxy Authentication Required'aiohttp.client_exceptions.ClientProxyConnectionError: Cannot connect to hostscrapy.core.downloader.handlers.http11.TunnelError: Could not open CONNECT tunnel with proxy
What it means
Your Python code never reached the website. Your request got stuck at the proxy (the middleman server your traffic goes through).
ProxyError means the proxy could not be reached, or it refused to open a line to the site.
SSLError with CERTIFICATE_VERIFY_FAILED behind a proxy means a machine in the middle showed a certificate your Python does not trust.
Why it happens
The real reason sits at the end, in the Caused by … part. Read it first.
- The proxy URL says
https://, but the proxy speaks plain HTTP. urllib3 then adds:Your proxy appears to only use HTTP and not HTTPS. The TLS message below it is oftenWRONG_VERSION_NUMBER. - Nothing answers at that address.
Connection refusedmeans a wrong host or port, or the proxy program is not up. - The proxy refused the tunnel.
Tunnel connection failed: 407means it wants a login (407 explained). Other numbers, such as503, mean it could not or would not reach the site. - Your login broke on its way. An
@or:in the password splits the proxy URL in the wrong place. - An old setting wins. An
HTTPS_PROXYenvironment variable, or even a system proxy on Windows or macOS, beats the proxies you set on a session object. - SOCKS without the extra package. The library needs
requests[socks], or it stops withMissing dependencies for SOCKS support. - A certificate sitting in the middle (the SSLError). Something between you and the site shows a certificate signed by its own CA, and Python doesn’t trust that CA:
- Unblocker APIs in proxy mode. Zyte and Bright Data hand you their own CA certificate (the “root” that vouches for others) to trust, while Oxylabs’ Python sample turns the check off.
- Debug proxies. mitmproxy makes a fake certificate for every site, issued by its own CA.
- Company proxies that inspect traffic, signed by a company CA that Python doesn’t trust.
- An expired vendor root. Bright Data’s old certificates on ports 22225 and 33335 expired on 25 Sep 2026.
A request through a proxy makes two hops: you to the proxy, then the proxy to the site. For an https:// site your client sends CONNECT example.com:443, waits for a 200, then sets up encryption (TLS) with the site through that tunnel.
The scheme in your proxy URL decides the first hop. http:// means plain talk to the proxy. https:// means TLS to the proxy first, then TLS to the site inside it (urllib3 calls it TLS-in-TLS). Point https:// at a plain proxy and the TLS handshake fails. urllib3 reads wrong version number, unknown protocol or record layer failure (the OpenSSL 3.2.0 wording) as a sure sign of this.
mitmproxy can only decrypt your traffic if your client trusts its CA. And requests trusts the certifi package, not your system’s store, so a CA your IT team installed on Windows or macOS is a stranger to it.
Fix it
- Read the
Caused bypart and match it against the list above. - Test the proxy alone with curl, outside Python:
curl -v -x http://proxy.example:8080 -U user:pass https://example.comIf curl works then the proxy is fine and your Python setup is the problem. - Start the proxy URL with
http://, for both thehttpandhttpskeys. Usehttps://only if your provider’s own docs say so. - Pass
proxies=on every call, and checkenv | grep _PROXYto spot old values. - Escape the login. Write
@as%40and:as%3A, or leturllib.parse.quote(password, safe="")do it. - For SOCKS:
pip install 'requests[socks]', then picksocks5h://so the proxy looks up the site’s address. - For the SSLError: trust the proxy’s CA instead of turning checks off:
- Unblocker API: add the vendor’s CA file to a CA bundle and point
REQUESTS_CA_BUNDLEat that file. - Bright Data: switch to port
44445withbrightdata_root_ca_44445.crt. - mitmproxy: trust
~/.mitmproxy/mitmproxy-ca-cert.pem. - Company proxy:
truststoremakes Python use your system’s certificate store, the one your IT team manages.
- Unblocker API: add the vendor’s CA file to a CA bundle and point
Curl first, because it takes Python out of the picture. With -v, curl prints the CONNECT line and the proxy’s answer with the status code included.
Why not verify=False: requests then accepts any certificate, expired ones and ones for the wrong host included. According to the requests docs, that opens you to a man-in-the-middle attack. Trust one extra CA instead. It costs one file and keeps verification on.
One trap with your own CA file: verify= and REQUESTS_CA_BUNDLE replace the default list, they don’t add to it. A file with only the vendor’s CA breaks every other HTTPS site you call. Zyte’s docs say to append its CA to a generic bundle for exactly this reason.
What if you cannot load a certificate at all? Bright Data says you can skip it with its proxy API, or by getting the account verified through its KYC check.
Still failing with one provider? Compare others and their prices:
| Vendor, plan it picks | Total a month | $/GB | Total a year | Free trial · note · source |
|---|---|---|---|---|
| HProxy Residential ProxiesLite: 10 GB | $4.70 | $0.47 | $56.40 | hproxy.com · 29 Nov 2026 |
| CakeIP10GB | $6.00 | $0.60 | $72 | promo pricecakeip.com · 29 Nov 2026 |
| Geonode Residential Proxies10 GB Plan | $7.92 | $0.79 | $95.04 | free trial: $5 = 10 GB for 3 daysgeonode.com · 29 Nov 2026 |
| ProxyBase Residential ProxiesPay as you go | $8.50 | $0.85 | $102 | proxybase.org · 29 Nov 2026 |
List prices from each vendor’s pricing page, US dollars before tax, live from our price data. Your own size, in the price calculator: Residential proxies.
Paste this into your AI
My Python scraper fails with this proxy error:
"<paste the full last line, including everything after 'Caused by'>"
Explain in plain words what it means and which hop failed: me to the proxy, the proxy to the site, or a certificate check.
List the likely causes in order, most likely first. For each, give one quick test and one fix.
Show the curl -v command that tests my proxy alone.
My setup: <requests / httpx / aiohttp / Scrapy> on Python <version>,
how I pass the proxy (<proxies dict / env var HTTPS_PROXY / meta['proxy']>),
proxy URL scheme: <http:// / https:// / socks5:// / socks5h://>,
proxy type: <provider proxy / unblocker API in proxy mode / corporate proxy / mitmproxy>,
verify setting: <default / path to a CA file / False>.
Never ask me to paste my real password.Dev details
Where the message comes from
urllib3’s errors get turned into requests’ own. A urllib3 ProxyError becomes requests.exceptions.ProxyError, and an SSLError becomes requests.exceptions.SSLError.
The exact wording changes with your urllib3 version:
| urllib3 | message |
|---|---|
| 2.x | ProxyError('Unable to connect to proxy', <cause>) |
2.x, https:// proxy URL + TLS error |
Unable to connect to proxy. Your proxy appears to only use HTTP and not HTTPS, … |
| 1.26.x | ProxyError('Cannot connect to proxy.', <cause>) |
urllib3 2 adds the HTTP-only hint when the TLS error contains wrong version number, unknown protocol or record layer failure, and the proxy URL is https://.
It wraps an error as ProxyError only if it happens before the connection to the proxy is up. A certificate failure at the site, once the tunnel is open, stays an SSLError.
Tunnel connection failed: … is Python’s own wording. http.client raises it when the proxy answers CONNECT with anything but 200.
Real messages
requests.exceptions.ProxyError: HTTPSConnectionPool(host='…web-security-academy.net', port=443): Max retries exceeded with url: / (Caused by ProxyError('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fd62b7fead0>: Failed to establish a new connection: [Errno 111] Connection refused')))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='…web-security-academy.net', port=443): Max retries exceeded with url: /login (Caused by ProxyError('Unable to connect to proxy. Your proxy appears to only use HTTP and not HTTPS, try changing your proxy URL to be HTTP. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#https-proxy-error-http-proxy', SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1125)'))))
(Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable')))
HTTPSConnectionPool(host='login.microsoftonline.com', port=443): Max retries exceeded with url: /organizations/v2.0/.well-known/openid-configuration (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Basic Constraints of CA cert not marked critical (_ssl.c:1032)')))
The proxies dict
import requests
from urllib.parse import quote
user, password = "user", quote("p@ss:word", safe="") # -> p%40ss%3Aword
proxy = f"http://{user}:{password}@proxy.example:8080" # http://, also for https targets
proxies = {"http": proxy, "https": proxy}
r = requests.get("https://example.com", proxies=proxies, timeout=30)
- The key names the target’s scheme, and the value’s scheme is how you reach the proxy.
- Proxy URLs have to include the scheme.
- requests URL-decodes the user and password, then sends them as a Basic
Proxy-Authorizationheader. session.proxiesis overwritten by environment proxies. Passingproxies=on each request avoids that.urllib.parse.quotekeeps/unless you also passsafe="".
To find a system-wide proxy (Windows or macOS), urllib3’s docs suggest:
python -c 'import urllib.request; print(urllib.request.getproxies())'
SOCKS
# pip install 'requests[socks]'
proxies = {
"http": "socks5h://user:pass@host:port",
"https": "socks5h://user:pass@host:port",
}
socks5 resolves names on your machine, socks5h on the proxy, same as curl does.
Trusting the proxy’s CA
verify= takes a path to a CA bundle. That file then replaces the default list, so build one that carries both:
python -c "from requests.utils import DEFAULT_CA_BUNDLE_PATH; print(DEFAULT_CA_BUNDLE_PATH)"
cat /path/printed/above zyte-ca.crt > ca-bundle.pem
export REQUESTS_CA_BUNDLE="$PWD/ca-bundle.pem" # CURL_CA_BUNDLE is the fallback
That is Zyte’s advice for requests: append its CA to a standard bundle and point REQUESTS_CA_BUNDLE at it.
What each vendor says:
| service | what its docs say |
|---|---|
Zyte API, proxy mode (api.zyte.com:8011) |
install the Zyte CA certificate for requests |
| Bright Data, native proxy mode | load brightdata_root_ca_44445.crt (curl: --cacert) on port 44445; ports 22225 and 33335 certificates expired 25 Sep 2026, 00:00 UTC |
Oxylabs Web Unblocker (unblock.oxylabs.io:60000) |
Python sample uses an https:// proxy URL and verify=False |
Bright Data adds that code that skips verification (curl -k) keeps working after the expiry, because the certificate is never checked.
verify=False accepts any certificate, expired or for the wrong host. Requests’ own docs warn this opens the door to man-in-the-middle attacks.
Company proxy, Python 3.10+:
import truststore
truststore.inject_into_ssl() # call as early as possible in your program
import requests
requests.get("https://example.com")
Python 3.13: “Basic Constraints of CA cert not marked critical”
Python 3.13 turned on VERIFY_X509_STRICT in ssl.create_default_context(). urllib3 2.4.0 (10 Apr 2025) does the same in its own contexts on Python 3.13+.
Strict mode can reject older (pre-RFC 5280) or malformed certificates that OpenSSL alone would accept. One report: an Azure CLI user behind a company proxy that inspects traffic, on Python 3.13.11, got exactly this error.
Python’s docs show how to switch it off, but advise against it:
ctx.verify_flags &= ~ssl.VERIFY_X509_STRICT
httpx, aiohttp, Scrapy
import httpx
with httpx.Client(proxy="http://user:pass@proxy.example:8080") as client:
client.get("https://example.com")
# failed CONNECT -> httpx.ProxyError: 407 Proxy Authentication Required
The HTTPX docs: “In most cases, the proxy URL for the https:// key should use the http:// scheme (that’s not a typo!)”. An https:// proxy URL to a plain proxy can end in httpx.ProxyError: _ssl.c:1091: The handshake operation timed out.
import asyncio
import aiohttp
async def main():
async with aiohttp.ClientSession() as session:
async with session.get("https://example.com",
proxy="http://user:pass@proxy.example:8080") as resp:
print(resp.status)
asyncio.run(main())
# failed CONNECT -> ClientHttpProxyError: 407, message='Proxy Authentication Required', url=…
# proxy unreachable -> ClientProxyConnectionError: Cannot connect to host …
- aiohttp ignores
HTTP_PROXYand friends unless you settrust_env=True. - A custom
sslcontext is used for the site and for an HTTPS proxy, so it must trust both.
# inside your spider's parse() (or start()) method
yield scrapy.Request(url, meta={"proxy": "http://user:pass@proxy.example:8080"})
# failed CONNECT -> TunnelError: Could not open CONNECT tunnel with proxy 104.120.33.32:12345 [{'status': 407, 'reason': b'Proxy Authentication Required'}]
Scrapy URL-decodes the username and password from meta["proxy"] and sends a Proxy-Authorization: Basic header.
The verify error names
text after certificate verify failed: |
what it means |
|---|---|
self-signed certificate in certificate chain |
OpenSSL: the chain was built, but its root is not in your trust store |
unable to get local issuer certificate |
OpenSSL: the issuer could not be found locally, so the trusted list is incomplete |
Basic Constraints of CA cert not marked critical |
Python 3.13+ strict check on the CA (above) |
Python builds the line as [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: <reason> (_ssl.c:<line>). The reason is OpenSSL’s own text for the verify code, copied word for word.
“Max retries exceeded” sounds like your code tried many times. Most likely it tried once: requests sets its adapters to zero retries by default (DEFAULT_RETRIES = 0), and urllib3 prints that phrase when its retry counter runs dry, even at zero.
The inner error shows you which hop broke:
OSError('Tunnel connection failed: …'): the proxy answered and refused.NewConnectionError: you never even reached the proxy.SSLErrornested insideProxyError: TLS to the proxy itself failed before the tunnel.- A plain
SSLError: the site’s certificate, as shown through the proxy, failed the check.
ProxyError and SSLError are both subclasses of requests.exceptions.ConnectionError, so except requests.ConnectionError catches both. Catch ProxyError first if you only want to switch proxies on proxy failures.
Questions
Why use http:// for the 'https' key in the proxies dict?
The key is the scheme of the site you visit. The value's scheme says how you talk to the proxy itself. Most proxies speak plain HTTP and open a tunnel for https:// sites, so the value starts with http://. The HTTPX docs call this a common gotcha: 'that's not a typo'. Use https:// only when your provider documents it, like Oxylabs Web Unblocker does.
Is verify=False safe with an unblocker API?
It makes your code accept any certificate, which the requests docs say leaves you open to man-in-the-middle attacks. Oxylabs' Python sample uses it. Zyte and Bright Data offer their own CA certificate instead. Trusting that CA keeps the check on.
What is the difference between socks5:// and socks5h://?
With socks5://, your machine looks up the site's address (DNS). With socks5h://, the proxy does it. The requests and urllib3 docs both point to socks5h when you want the proxy to resolve names.
scrapenews difficulty
Not scored. We have 1 of 8 factors; a score needs 3.
| Factor | What we found | Source |
|---|---|---|
| Smallest client that gets through | a plain HTTP client | python-httpx.org |
Our analysis of 1 sourced facts; measured runs replace them as they come. How we rate difficulty
Related
- Python web scraping Guide
- Free proxy for web scraping Guide
- Rotating proxies Guide
- Residential proxies Compare
- Proxy checker Free tool
- SSL certificate verify failed Error
Tools that get past it
| Tool | Price |
|---|---|
| $1.50/1k req | |
| HTTPX: Python HTTP client with sync and async APIs and HTTP/2 support. | Free |
| $7.20/GB | |
| Free | |
| Free | |
| $0.13/1k req |
Sources
Requests docs: Proxies (env vars, CA bundle for https proxies, SOCKS, socks5h) requests.readthedocs.io
Requests docs: SSL Cert Verification and CA Certificates requests.readthedocs.io
psf/requests source: adapters.py (ProxyError, SSLError, SOCKS dependency error) github.com
psf/requests source: exceptions.py (ProxyError and SSLError subclass ConnectionError) github.com
urllib3 issue #3268: OpenSSL 3.2.0 'record layer failure' message github.com
psf/requests source: utils.py (get_auth_from_url unquotes the login) github.com
urllib3 source: connection.py (_wrap_proxy_error, 'Unable to connect to proxy') github.com
urllib3 source: connectionpool.py (which errors become ProxyError) github.com
urllib3 1.26.x source: connectionpool.py ('Cannot connect to proxy.') github.com
urllib3 docs: Proxies, and 'Your proxy appears to only use HTTP and not HTTPS' urllib3.readthedocs.io
urllib3 source: util/ssl_.py (VERIFY_X509_STRICT on Python 3.13+) github.com
urllib3 changelog: 2.4.0 (2025-04-10) verify_flags default github.com
CPython 3.13 source: http/client.py ('Tunnel connection failed') github.com
Python docs: What's New in 3.13 (ssl VERIFY_X509_STRICT default) docs.python.org
Python docs: urllib.parse.quote docs.python.org- HTTPX docs: Proxies (http:// scheme gotcha) python-httpx.org
- HTTPX docs: Troubleshooting proxies python-httpx.org
httpcore source: http_proxy.py (ProxyError on a failed CONNECT) github.com
aiohttp docs: Proxy support docs.aiohttp.org
aiohttp source: client_exceptions.py (ClientHttpProxyError, ClientProxyConnectionError) github.com
Scrapy source: http11.py (TunnelError) github.com
Scrapy source: httpproxy.py (meta['proxy'], Proxy-Authorization) github.com
aivarsk/scrapy-proxies issue #63: TunnelError with 407 github.com
shakenfist issue #5002: ProxyError 'Tunnel connection failed: 503 Service Unavailable' github.com
Web-Security-Academy-Series issue #21: ProxyError, Connection refused github.com
Web-Security-Academy-Series issue #20: 'Your proxy appears to only use HTTP', WRONG_VERSION_NUMBER github.com
Azure CLI issue #32749: 'Basic Constraints of CA cert not marked critical' behind a corporate proxy github.com
CPython source: Modules/_ssl.c (SSLError message format, verify reason from OpenSSL) github.com- OpenSSL docs: certificate verify error strings docs.openssl.org
Oxylabs Docs: Web Unblocker, Making Requests (verify=False sample) developers.oxylabs.io
Bright Data Docs: SSL certificate (port 44445) docs.brightdata.com
Bright Data Docs: Root certificate migration (old certificates expired 25 Sep 2026) docs.brightdata.com
Zyte docs: Zyte API proxy mode docs.zyte.com
Zyte docs: Installing the Zyte CA certificate (CA bundle, REQUESTS_CA_BUNDLE) docs.zyte.com
mitmproxy docs: About Certificates docs.mitmproxy.org
Truststore docs: use the system certificate store in Python truststore.readthedocs.io
curl man page: -x, -U, --cacert curl.se