Python requests library combine HTTPProxyAuth with HTTPBasicAuth

4.6k Views Asked by At

Found an example on HTTPProxyAuth usage here https://stackoverflow.com/a/8862633

But I'm hoping for a sample on usage with both HTTPProxyAuth and HTTPBasicAuth IE I need to pass a server, username and password through the proxy and a username and password to a web page...

Thanks in advance.

Richard

3

There are 3 best solutions below

0
On

For Basic Authentication you can Httplib2 module of python. An example is given below. For more details check this

>>>import httplib2

>>>h = httplib2.Http(".cache")

>>>h.add_credentials('name', 'password')

>>>resp, content = h.request("https://example.org/chap/2", 
"PUT", body="This is text", 
headers={'content-type':'text/plain'} )

I don't think Httplib2 provides the proxy support. Check the link -

0
On

Unfortunately, HTTPProxyAuth is a child of HTTPBasicAuth and overrides its behaviour (please see requests/auth.py).

However, you can add both the required headers to your request by making a new class that implements both behaviours:

class HTTPBasicAndProxyAuth:
    def __init__(self, basic_up, proxy_up):
        # basic_up is a tuple with username, password
        self.basic_auth = HTTPBasicAuth(*basic_up)
        # proxy_up is a tuple with proxy username, password
        self.proxy_auth = HTTPProxyAuth(*proxy_up)

    def __call__(self, r):
        # this emulates what basicauth and proxyauth do in their __call__()
        # first add r.headers['Authorization']
        r = self.basic_auth(r)
        # then add r.headers['Proxy-Authorization']
        r = self.proxy_auth(r)
        # and return the request, as the auth object should do
        return r
0
On

It's not pretty but you can provide separate BasicAuth credentials in both the Proxy AND the restricted page URLs.

For example:

proxies = {
 "http": "http://myproxyusername:mysecret@webproxy:8080/",
 "https": "http://myproxyusername:mysecret@webproxy:8080/",
}

r = requests.get("http://mysiteloginname:[email protected]", proxies=proxies)