17

I've been struggling with my company proxy to make an https request.

import requests
from requests.auth import HTTPProxyAuth

proxy_string = 'http://user:password@url_proxt:port_proxy'

s = requests.Session()
s.proxies = {"http": proxy_string , "https": proxy_string}
s.auth = HTTPProxyAuth(user,password)

r = s.get('http://www.google.com') # OK
print(r.text)
r = s.get('https://www.google.com',proxies={"http": proxy_string , "https": proxy_string}) #OK
print(r.text)
r = s.get('https://www.google.com') # KO
print(r.text)

When KO, I have the following exception :

HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',)))

I looked online but didn't find someone having this specific issue with HTTPS.

Thank you for your time

0

4 Answers 4

9

Thanks to the amazing help of Lukasa, I solved my issue.

Please see discussion on fix here or set :

session.trust_env=False
Sign up to request clarification or add additional context in comments.

1 Comment

Did not work for me !
1

I personally solved the above problem on my system by updating the environment variables http_proxy,https_proxy,socks_proxy,ftp_proxy.

First enter the command on your terminal : printenv

This should show you the environment variables on your system.

In my case intially: http_proxy=http://proxyserver:port/

I changed it to : http_proxy=http://username:password@proxy:port/

using the command export http_proxy="http://username:password@proxy:port/"

Similarly for https_proxy,socks_proxy,ftp_proxy

Comments

1

Other way i have resolved is - speak with your corporate IT administrator and find a direct proxy port which connects to external domain (with / without password)

pip install --proxy=http://proxyhost:proxy_port pixiedust

Found from other colleagues using the proxy (proxy_port direct connection) in their eclipse settings (network)

Comments

1

To anyone else that tried the accepted answer's "session.trust_env=False" with no success, there may be a deeper issue that produces a similar error (which is probably not the issue the OP had): There may be a corporate proxy configuration that requires specific headers to be sent upon CONNECT, and python requests doesn't send them ('User-Agent' and 'Host', for example).

I do not have a solution for that at the moment. See https://github.com/psf/requests/issues/5028 for a discussion on the subject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.