Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests API using proxy for https request get 407 Proxy Authentication Required

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

like image 364
Fabrice E. Avatar asked Dec 01 '15 17:12

Fabrice E.


People also ask

How to use proxies in Python requests library?

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.

Is there a way to authenticate with a HTTP proxy in Python?

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.

Can I skip the proxy password for HTTP requests?

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!

Why am I getting a 407 error when using requests-toolbelt?

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 ).


1 Answers

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

Please see discussion on fix here or set :

session.trust_env=False
like image 82
Fabrice E. Avatar answered Sep 28 '22 04:09

Fabrice E.