简体   繁体   English

Urllib2(Python 2.7)中的代理身份验证错误

[英]Proxy Authentication error in Urllib2 (Python 2.7)

[Windows 7 64 bit; [Windows 7 64位; Python 2.7] Python 2.7]

If I try to use Urllib2, I get this error 如果我尝试使用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**

Now, I'm behind a college proxy which requires authentication so that's probably the reason why this is happening. 现在,我落后于一个需要身份验证的大学代理,这可能就是这种情况发生的原因。 But isn't Urllib2 supposed to pull the authentication and proxy information from the system settings? 但是,Urllib2是否不应该从系统设置中提取身份验证和代理信息?

I understand there's some extra code I can insert into my program to 'hardcode' the proxy information in the program but I really don't want to do that unless it's the last resort. 我知道我可以在程序中插入一些额外的代码来“硬编码”程序中的代理信息,但是我真的不想这样做,除非万不得已。 It would hinder the portability of the program across computers with different authentication IDs and Passwords in the college. 这会阻碍程序在学院中具有不同身份验证ID和密码的计算机之间的可移植性。

Your program should see the environment variables which are set in Windows. 您的程序应该看到在Windows中设置的环境变量。 So have these two environment variables in your Windows. 因此,在Windows中有这两个环境变量。

HTTP_PROXY = http://username:password@proxyserver.domain.com

HTTPS_PROXY = https://username:password@proxyserver.domain.com

And go ahead with executing your script. 并继续执行脚本。 It should pick up the proper authenticators and proceed with the connection. 它应该选择适当的身份验证器并继续进行连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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