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
Python requests library accepts an argument proxies which takes the proxy details before making an api call. Let us take a look at the below example. In this example, we are using a GET api call but the same proxy setup is applicable to all the other methods like POST, PUT, DELETE etc. Change the urls, port to match your proxy urls.
I've written a Python module (available here) which makes it possible to authenticate with a HTTP proxy using the digest scheme. It works when connecting to HTTPS websites (through monkey patching) and allows to authenticate with the website as well. This should work with latest requests library for both Python 2 and 3.
You can skip the proxy password if the default API parameters suit your needs.: Remember that if you want to use proxy mode, your code must be configured not to verify SSL certificates. In this case, it would be verify=False since you are working with Python Requests. That's all there is to sending successful HTTP requests!
You can still get a 407 if proxy credentials are wrong, so check that maybe. Thank you very much for sharing your Python module! This works great, and works great when connecting to HTTPS site, where requests-toolbelt fails. Thank you so much! This snippet works for both types of requests ( http and https ).
Thanks to the amazing help of Lukasa, I solved my issue.
Please see discussion on fix here or set :
session.trust_env=False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With