Python Requests - 407 Proxy Authentication Required

Published
Updated

A 407 Proxy Authentication Required response is often returned either when a request being made through a proxy requires authentication or if the credentials being passed are invalid or have been incorrectly escaped. The following are a number of different approaches on how you can fix this issue, as some proxies require different implementations.

Proxy Authentication using Requests’ Session Feature

Python’s requests library lets you easily configure the proxy on a per-request basis using a session object.

import requests
from urllib3.util import parse_url, Url
from urllib.parse import quote

proxy_url_credentials = self.add_creds_to_proxy_url('http://someproxyurl.com:8080/', 'some_user', 'some_password')

proxies = {'http': proxy_url_credentials, 'https': proxy_url_credentials}
session = requests.session()
session.proxies.update(proxies)

response = session.get('https://google.com/', verify=False)

print(response.text)

def add_creds_to_proxy_url(self, url, username, password)
    url_dict = parse_url(url)._asdict()
    url_dict['auth'] = username + ':' + quote(password, '')
    return Url(**url_dict).url

The above code will configure your proxy url so that the credentials are included and the password has been percent-encoded. This is important because passwords can often contain various symbols that are not permitted in an HTTP request. This often results in errors such as:

requests.exceptions.ProxyError
Max retries exceeded with url:
Failed to establish a new connection 

[Errno -5] No address associated with hostname
[Errno -3] Temporary failure in name resolution