Urllib2 (Python 2.7) 中的代理身份验证错误 [英] Proxy Authentication error in Urllib2 (Python 2.7)

查看:44
本文介绍了Urllib2 (Python 2.7) 中的代理身份验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Windows 7 64 位;Python 2.7]

如果我尝试使用 Urllib2,我会收到此错误

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

现在,我支持需要身份验证的大学代理,所以这可能是发生这种情况的原因.但是 Urllib2 不应该从系统设置中提取身份验证和代理信息吗?

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?

我知道我可以在我的程序中插入一些额外的代码来硬编码"程序中的代理信息,但我真的不想这样做,除非这是最后的手段.这会阻碍程序在学院内不同身份验证ID和密码的计算机之间的可移植性.

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.

推荐答案

您的程序应该可以看到在 Windows 中设置的环境变量.所以在你的 Windows 中有这两个环境变量.

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.

这篇关于Urllib2 (Python 2.7) 中的代理身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆