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

1 回答 1

4

找出问题所在,必须在标头中设置 Proxy-Authorization 字段:

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))
于 2012-06-22T20:41:47.500 回答