Python request library combines HTTPProxyAuth with HTTPBasicAuth

Found an example of using HTTPProxyAuth here https://stackoverflow.com/a/316618/

But I hope for a sample for use with HTTPProxyAuth and HTTPBasicAuth IE. I need to pass the server, username and password through a proxy and username and password to the web page ...

Thanks in advance.

Richard

+4
source share
3 answers

For basic authentication, you can use the httplib2 module for python. The following is an example. See this for more details.

>>>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 do not think Httplib2 provides proxy support. Check link -

+1
source

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

However, you can add both required headers to your query by creating a new class that implements both behaviors:

 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
source

This is not very good, but you can provide separate BasicAuth credentials both in proxies and in URLs with limited URLs.

For instance:

 proxies = { "http": "http://myproxyusername: mysecret@webproxy :8080/", "https": "http://myproxyusername: mysecret@webproxy :8080/", } r = requests.get("http://mysiteloginname: myothersecret@mysite.com ", proxies=proxies) 
0
source

Source: https://habr.com/ru/post/1393266/


All Articles