代理用户名/密码与Twisted

时间:2012-06-22 19:13:16

标签: python proxy twisted

我正在尝试使用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)

1 个答案:

答案 0 :(得分: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))