Note that there are some explanatory texts on larger screens.

plurals
  1. POrequests library https get via proxy leads to error
    primarykey
    data
    text
    <p>Trying to send a simple get request via a proxy. I have the 'Proxy-Authorization' and 'Authorization' headers, don't think I needed the 'Authorization' header, but added it anyway.</p> <pre><code>import requests URL = 'https://www.google.com' sess = requests.Session() user = 'someuser' password = 'somepass' token = base64.encodestring('%s:%s'%(user,password)).strip() sess.headers.update({'Proxy-Authorization':'Basic %s'%token}) sess.headers['Authorization'] = 'Basic %s'%token resp = sess.get(URL) </code></pre> <p>I get the following error:</p> <pre><code>requests.packages.urllib3.exceptions.ProxyError: Cannot connect to proxy. Socket error: Tunnel connection failed: 407 Proxy Authentication Required. </code></pre> <p>However when I change the URL to simple <code>http://www.google.com</code>, it works fine.</p> <p>Do proxies use Basic, Digest, or some other sort of authentication for https? Is it proxy server specific? How do I discover that info? I need to achieve this using the requests library.</p> <p><strong>UPDATE</strong></p> <p>Its seems that with HTTP requests we have to pass in a <code>Proxy-Authorization</code> header, but with HTTPS requests, we need to format the proxy URL with the username and password</p> <pre><code>#HTTP import requests, base64 URL = 'http://www.google.com' user = &lt;username&gt; password = &lt;password&gt; proxy = {'http': 'http://&lt;IP&gt;:&lt;PORT&gt;} token = base64.encodestring('%s:%s' %(user, password)).strip() myheader = {'Proxy-Authorization': 'Basic %s' %token} r = requests.get(URL, proxies = proxies, headers = myheader) print r.status_code # 200 #HTTPS import requests URL = 'https://www.google.com' user = &lt;username&gt; password = &lt;password&gt; proxy = {'http': 'http://&lt;user&gt;:&lt;password&gt;@&lt;IP&gt;:&lt;PORT&gt;} r = requests.get(URL, proxies = proxy) print r.status_code # 200 </code></pre> <p>When sending an HTTP request, if I leave out the header and pass in a proxy formatted with user/pass, I get a 407 response.</p> <p>When sending an HTTPS request, if I pass in the header and leave the proxy unformatted I get a ProxyError mentioned earlier.</p> <p>I am using requests 2.0.0, and a Squid proxy-caching web server. Why doesn't the header option work for HTTPS? Why does the formatted proxy not work for HTTP?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload