从公司防火墙后面使用 urllib2 打开网站 - 11004 getaddrinfo 失败
我正在尝试使用以下方法从公司防火墙后面访问网站:-
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, username, password)
auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
conn = urllib2.urlopen('http://python.org')
出现错误
URLError: <urlopen error [Errno 11004] getaddrinfo failed>
我尝试过使用不同的处理程序(也以稍微不同的方式尝试过 ProxyHandler),但似乎不起作用。
有什么线索可以说明错误的原因是什么以及提供凭据并使其正常工作的任何不同方法吗?
I am trying to access a website from behind corporate firewall using below:-
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, username, password)
auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
conn = urllib2.urlopen('http://python.org')
Getting error
URLError: <urlopen error [Errno 11004] getaddrinfo failed>
I have tried with different handlers (tried ProxyHandler also in slightly different way), but doesn't seem to work.
Any clues to what could be the reason for error and any different ways to supply the credentials and make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用代理并且该代理具有用户名和密码(许多公司代理都有),则需要使用 urllib2 设置代理处理程序。
HTTPBasicAuthHandler 用于为您要访问的站点提供凭据,而不是通过代理。上面的片段可能对您有帮助。
If you are using Proxy and that proxy has Username and Password (which many corporate proxies have), you need to set the proxy handler with urllib2.
HTTPBasicAuthHandler is used to provide credentials for the site which you are going to access and not for going through the proxy. The above snippet might help you.
在 Windows 上,我观察到 python 使用
IE Internet Options-> LAN 设置
设置。因此,即使我们使用
urllib2
安装opener并指定proxy_url
,它也会继续使用IE设置。当我导出系统变量时,它最终工作正常:
On Windows, I observed that python uses the
IE Internet Options-> LAN Settings
settings.So even if we use
urllib2
to install opener and specify theproxy_url
, it would continue to use the IE settings.It worked fine finally, when I exported a system variable: