2012-01-26 63 views
4

發現的例子在HTTPProxyAuth使用這裏https://stackoverflow.com/a/8862633Python的請求庫結合HTTPProxyAuth與HTTPBasicAuth

但我希望有一個樣品上使用既HTTPProxyAuth和HTTPBasicAuth IE我需要通過一個服務器,用戶名和密碼代理和用戶名和密碼到網頁...

在此先感謝。

Richard

+0

什麼是你要求? –

+0

我需要通過添加HTTPBasicAuth來構建鏈接中的示例,因爲我不僅需要通過帶有用戶名\密碼的代理來獲取代碼,還需要使用不同用戶名\密碼對目標網頁進行基本身份驗證。 – thylacine

回答

1

對於基本身份驗證,您可以使用python的Httplib2模塊。下面給出一個例子。有關詳情,請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'}) 

我不認爲httplib2都提供代理支持。檢查link -

0

不幸的是,HTTPProxyAuthHTTPBasicAuth的孩子,並且覆蓋其行爲(請參閱requests/auth.py)。

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

它不漂亮,但你可以在兩個代理和限制提供單獨的基本驗證憑據:

但是,你可以通過同時實現行爲的新類同時添加所需的頭部您的要求頁面網址。

例如:

proxies = { 
"http": "http://myproxyusername:[email protected]:8080/", 
"https": "http://myproxyusername:[email protected]:8080/", 
} 

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