4

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

Community
  • 1
  • 1
thylacine
  • 61
  • 1
  • 5
  • What are you asking for? – Stephen Gross Jan 26 '12 at 22:35
  • I need to build on the sample in the link by adding HTTPBasicAuth because not only do i need to get thru the proxy with username\password I also need to basic authenticate with the target webpage with a different username\password. – thylacine Jan 27 '12 at 03:21

3 Answers3

1

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 -

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Ameet
  • 37
  • 9
0

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
Charl Botha
  • 4,373
  • 34
  • 53
0

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:myothersecret@mysite.com", proxies=proxies)
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100