title: requests return "Tunnel connection failed: 407 Proxy Authentication Required" although proxy credentials are correct
body: <p>I have to retrieve a webpage using a proxy which requires authentication (HTTP Basic Auth).</p>
  <p>Even though the credentials are correct, requests return a &quot;407 Proxy Authentication Required&quot; error.</p>
  <p>In particular:</p>
  <pre><code>import requests
  proxies = {
    'http': 'http://MYUSERNAME:MYPASSWORD@proxy.polimi.it:8080',
    'https': 'http://MYUSERNAME:MYPASSWORD@proxy.polimi.it:8080'
  }
  url = &quot;https://www.sciencedirect.com/science/article/pii/S0048733321002481/pdfft?md5=af22e6c5d84527cb3d54f7feb17f0b54&amp;pid=1-s2.0-S0048733321002481-main.pdf&quot;
  s = requests.Session()
  s.trust_env=False
  </code></pre>
  <p>Then this code (without proxy):</p>
  <pre><code>res = s.get(url)
  print(res.status_code)
  </code></pre>
  <p>returns a 403 (Forbidden) status code.</p>
  <p>But the code with the proxy throws an exception:</p>
  <pre><code>res = s.get(url, proxies=proxies)
  print(res.status_code)
  </code></pre>
  <p>The exception is as follows:</p>
  <pre><code>---------------------------------------------------------------------------
  OSError                                   Traceback (most recent call last)
  /usr/lib/python3.10/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
      695             if is_new_proxy_conn and http_tunnel_required:
  --&gt; 696                 self._prepare_proxy(conn)
      697 
  
  /usr/lib/python3.10/site-packages/urllib3/connectionpool.py in _prepare_proxy(self, conn)
      963 
  --&gt; 964         conn.connect()
      965 
  
  /usr/lib/python3.10/site-packages/urllib3/connection.py in connect(self)
      365             # self._tunnel_host below.
  --&gt; 366             self._tunnel()
      367             # Mark this connection as not reusable
  
  /usr/lib64/python3.10/http/client.py in _tunnel(self)
      922             self.close()
  --&gt; 923             raise OSError(f&quot;Tunnel connection failed: {code} {message.strip()}&quot;)
      924         while True:
  
  OSError: Tunnel connection failed: 407 Proxy Authentication Required
  
  During handling of the above exception, another exception occurred:
  
  MaxRetryError                             Traceback (most recent call last)
  /usr/lib/python3.10/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
      438             if not chunked:
  --&gt; 439                 resp = conn.urlopen(
      440                     method=request.method,
  
  /usr/lib/python3.10/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
      754 
  --&gt; 755             retries = retries.increment(
      756                 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  
  /usr/lib/python3.10/site-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
      573         if new_retry.is_exhausted():
  --&gt; 574             raise MaxRetryError(_pool, url, error or ResponseError(cause))
      575 
  
  MaxRetryError: HTTPSConnectionPool(host='www.sciencedirect.com', port=443): Max retries exceeded with url: /science/article/pii/S0048733321002481/pdfft?md5=af22e6c5d84527cb3d54f7feb17f0b54&amp;pid=1-s2.0-S0048733321002481-main.pdf (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required')))
  
  During handling of the above exception, another exception occurred:
  
  ProxyError                                Traceback (most recent call last)
  /tmp/ipykernel_25573/2629631255.py in &lt;module&gt;
  ----&gt; 1 res = s.get(url, proxies=proxies)
        2 print(res.status_code)
  
  /usr/lib/python3.10/site-packages/requests/sessions.py in get(self, url, **kwargs)
      553 
      554         kwargs.setdefault('allow_redirects', True)
  --&gt; 555         return self.request('GET', url, **kwargs)
      556 
      557     def options(self, url, **kwargs):
  
  /usr/lib/python3.10/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
      540         }
      541         send_kwargs.update(settings)
  --&gt; 542         resp = self.send(prep, **send_kwargs)
      543 
      544         return resp
  
  /usr/lib/python3.10/site-packages/requests/sessions.py in send(self, request, **kwargs)
      653 
      654         # Send the request
  --&gt; 655         r = adapter.send(request, **kwargs)
      656 
      657         # Total elapsed time of the request (approximately)
  
  /usr/lib/python3.10/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
      508 
      509             if isinstance(e.reason, _ProxyError):
  --&gt; 510                 raise ProxyError(e, request=request)
      511 
      512             if isinstance(e.reason, _SSLError):
  
  ProxyError: HTTPSConnectionPool(host='www.sciencedirect.com', port=443): Max retries exceeded with url: /science/article/pii/S0048733321002481/pdfft?md5=af22e6c5d84527cb3d54f7feb17f0b54&amp;pid=1-s2.0-S0048733321002481-main.pdf (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required')))
  </code></pre>