简体   繁体   中英

Proxy username/password with Twisted

I'm trying to use Twisted's ProxyAgent class to connect to a proxy server and make HTTP requests, however the server requires a username and password. Is it possible to specify these credentials to the server using ProxyAgent?

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

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

Figured out the problem, the Proxy-Authorization field has to be set in the headers:

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM