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