Python requests gets a 403 from the proxy before it reaches the site
Your error
requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.example.com', port=443): Max retries exceeded with url: /v1/items (Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 403 Forbidden')))1 more line it can showFix it fast
Most common cause, 1 of 3requests uses a proxy from the environment that you did not choose
If the host should be reached directly, list it in NO_PROXY or stop the session from reading the environment.
import requests
s = requests.Session()
s.trust_env = False # ignore HTTP(S)_PROXY and friends
r = s.get("https://api.example.com/v1/items", timeout=10)
# or keep the env proxies but skip one host:
# export NO_PROXY="api.example.com"Short answer
ProxyError with Tunnel connection failed: 403 Forbidden means the proxy, not the website, refused the HTTPS tunnel. Either requests took an unwanted proxy from HTTPS_PROXY, or the proxy's rules deny your client address or the target port. Exclude the host with NO_PROXY or fix the proxy's access rules. Applies to requests 2.34 and urllib3 2.8.
Which cause do you have?
Print the proxies Python reads from the environment, then repeat the request with curl through the same proxy and watch the reply to CONNECT. An entry you did not expect points to cause 1; a 403 to CONNECT on port 443 from your machine points to cause 2; a 403 only for another port points to cause 3.
python -c "import urllib.request; print(urllib.request.getproxies())"
curl -v --proxy http://10.10.1.10:3128 https://api.example.com/Then find the line you see in this list and open its fix:
- 1requests uses a proxy from the environment that you did not choose
Your code passes no proxies argument, yet getproxies() prints an https entry from HTTPS_PROXY or ALL_PROXY - 2The proxy's access rules do not allow your client
curl through the proxy also gets 403 to CONNECT for any HTTPS site on port 443, while the same command run on the proxy host works - 3The target port is not on the proxy's list of TLS ports
Requests to port 443 pass the proxy, but a URL such as https://api.example.com:8443/ fails with 403
What does the error look like?
requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.example.com', port=443): Max retries exceeded with url: /v1/items (Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 403 Forbidden')))OSError: Tunnel connection failed: 403 Forbidden
Cause 1 of 3
requests uses a proxy from the environment that you did not choose
You seeYour code passes no proxies argument, yet getproxies() prints an https entry from HTTPS_PROXY or ALL_PROXY
Check
When a request does not set proxies itself, requests takes the proxy from the http_proxy, https_proxy, all_proxy and no_proxy variables, upper or lower case. A corporate shell profile, a CI image or a container can set them, and that proxy then refuses the destination with 403. A dict in session.proxies does not win, because environment proxies overwrite it.
env | grep -i _proxyFix
If the host should be reached directly, list it in NO_PROXY or stop the session from reading the environment. If a different proxy is the right one, pass it in the proxies argument of each request, which the requests docs name as the way to beat environment settings.
import requests
s = requests.Session()
s.trust_env = False # ignore HTTP(S)_PROXY and friends
r = s.get("https://api.example.com/v1/items", timeout=10)
# or keep the env proxies but skip one host:
# export NO_PROXY="api.example.com"Verify
The request returns the site's own status code, and the ProxyError is gone.
python -c "import requests; s=requests.Session(); s.trust_env=False; print(s.get('https://api.example.com/', timeout=10).status_code)"Confirmed by requests.readthedocs.io
Cause 2 of 3
The proxy's access rules do not allow your client
You seecurl through the proxy also gets 403 to CONNECT for any HTTPS site on port 443, while the same command run on the proxy host works
Check
Squid's suggested squid.conf only lets localhost through and ends with http_access deny all; the allow localnet line ships commented out. Every refused request receives 403 Forbidden unless deny_info sets another code, so a client outside the allowed networks sees exactly this tunnel failure.
grep -nE "^(acl|http_access)" squid.confFix
If you run the proxy, define an acl for your client network and add an http_access allow line for it above deny all, then reload Squid. If someone else runs it, send them your public IP and the target host; there is nothing to change in the Python code.
# squid.conf, above "http_access deny all"
acl office src 203.0.113.0/24
http_access allow officeVerify
curl -v through the proxy no longer shows a 403 reply to CONNECT, and the Python request succeeds.
curl -v --proxy http://10.10.1.10:3128 https://api.example.com/Confirmed by squid-cache.org
Cause 3 of 3
The target port is not on the proxy's list of TLS ports
You seeRequests to port 443 pass the proxy, but a URL such as https://api.example.com:8443/ fails with 403
Check
The default Squid rules include http_access deny CONNECT !SSL_ports, and the stock SSL_ports acl lists only port 443. A tunnel to any other port is refused before the target server is contacted.
grep -n "SSL_ports" squid.confFix
Add the port to SSL_ports on a proxy you control, or ask its owner to allow it. Serving the API on 443 avoids the problem for every client behind such proxies.
# squid.conf
acl SSL_ports port 443
acl SSL_ports port 8443Verify
curl -v to the 8443 URL gets past the proxy, and requests returns the API's response.
curl -v --proxy http://10.10.1.10:3128 https://api.example.com:8443/Confirmed by squid-cache.org
How do I stop it coming back?
- Log urllib.request.getproxies() once at service start-up, so a proxy inherited from the host shows up in the logs.
- Set NO_PROXY for internal and partner API hosts in the same place where HTTPS_PROXY is set for the machine or container.
- When a service must use a proxy, pass proxies per request from config instead of relying on whatever the environment holds.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.