[Windows 7 64位; Python 2.7]
如果我尝试使用Urllib2,我得到这个错误
Traceback (most recent call last): File "C:\Users\cYanide\Documents\Python Challenge\1\1.py", line 7, in <module> response = urllib2.urlopen('http://python.org/') File "C:\Python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 400, in open response = meth(req, response) File "C:\Python27\lib\urllib2.py", line 513, in http_response 'http', request, response, code, msg, hdrs) File "C:\Python27\lib\urllib2.py", line 438, in error return self._call_chain(*args) File "C:\Python27\lib\urllib2.py", line 372, in _call_chain result = func(*args) File "C:\Python27\lib\urllib2.py", line 521, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) **urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required**
现在,我在一个需要authentication的大学代理的背后,这可能就是这种情况发生的原因。 但是不是Urllib2应该从系统设置拉取身份validation和代理信息?
我知道有一些额外的代码可以插入我的程序中,以便在程序中“硬编码”代理信息,但是除非是最后的手段,否则我真的不想这样做。 这将阻碍程序在具有不同authenticationID和密码的计算机上的可移植性。
您的程序应该看到在Windows中设置的环境变量。 所以在你的Windows中有这两个环境变量。
HTTP_PROXY = http://username:password@proxyserver.domain.com
HTTPS_PROXY = https://username:password@proxyserver.domain.com
并继续执行您的脚本。 它应该拿起适当的验证器并继续进行连接。