2012-06-22 96 views
3

我試圖使用Twisted的ProxyAgent類連接到代理服務器併發出HTTP請求,但服務器需要用戶名和密碼。是否可以使用ProxyAgent將這些憑證指定給服務器?代理用戶名/密碼與扭曲

endpoint = TCP4ClientEndpoint(reactor, host, port) 
agent = ProxyAgent(endpoint) 

# Maybe need to pass auth credentials in the header here? 
body = agent.request("GET", path) 

回答

4

想通了這個問題,代理授權場在頭被設置:

endpoint = TCP4ClientEndpoint(reactor, host, port) 
agent = ProxyAgent(endpoint) 

headers = {} 
auth = base64.b64encode("%s:%s" % (username, password)) 
headers["Proxy-Authorization"] = ["Basic " + auth.strip()] 

body = agent.request("GET", path, Headers(headers))