Proxy Authentication error in Urllib2 (Python 2.7)

我只是一个虾纸丫 提交于 2019-12-10 16:32:54

问题


[Windows 7 64 bit; Python 2.7]

If I try to use Urllib2, I get this error

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?

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.


回答1:


Your program should see the environment variables which are set in Windows. So have these two environment variables in your 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.



来源:https://stackoverflow.com/questions/12520790/proxy-authentication-error-in-urllib2-python-2-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!