python httplib:通过带身份验证的代理连接

python httplib: connect through proxy with authentification

我正在尝试通过具有身份验证的代理发送 GET 请求。 我有以下现有代码:

import httplib

username = 'myname'
password = '1234'
proxyserver = "136.137.138.139"
url       = "http://google.com"
c         = httplib.HTTPConnection(proxyserver, 83, timeout = 30)
c.connect()  
c.request("GET", url)
resp = c.getresponse()
data = resp.read()
print data

当 运行 这段代码时,我从代理那里得到一个答案,说我必须提供身份验证,这是正确的。 在我的代码中,我不使用登录名和密码。我的问题是我不知道如何使用它们!

有什么想法吗?

如果你特别想使用httplib,你可以参考这段代码。 https://gist.github.com/beugley/13dd4cba88a19169bcb0

但您也可以使用更简单的 requests 模块。

import requests
proxies = {
    "http": "http://username:password@proxyserver:port/",
    # "https": "https://username:password@proxyserver:port/",
}
url = 'http://google.com'
data = requests.get(url, proxies=proxies)