从公司防火墙后面使用 urllib2 打开网站 - 11004 getaddrinfo 失败

发布于 2024-10-15 04:48:41 字数 561 浏览 7 评论 0原文

我正在尝试使用以下方法从公司防火墙后面访问网站:-

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情场扛把子 2024-10-22 04:48:41

如果您使用代理并且该代理具有用户名和密码(许多公司代理都有),则需要使用 urllib2 设置代理处理程序。

  proxy_url = 'http://' + proxy_user + ':' + proxy_password + '@' + proxy_ip
  proxy_support = urllib2.ProxyHandler({"http":proxy_url})
  opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler)
  urllib2.install_opener(opener)

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.

  proxy_url = 'http://' + proxy_user + ':' + proxy_password + '@' + proxy_ip
  proxy_support = urllib2.ProxyHandler({"http":proxy_url})
  opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler)
  urllib2.install_opener(opener)

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.

剑心龙吟 2024-10-22 04:48:41

在 Windows 上,我观察到 python 使用 IE Internet Options-> LAN 设置 设置。
因此,即使我们使用urllib2安装opener并指定proxy_url,它也会继续使用IE设置。

当我导出系统变量时,它最终工作正常:

http_proxy=http://userid:[email protected]:port

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 the proxy_url, it would continue to use the IE settings.

It worked fine finally, when I exported a system variable:

http_proxy=http://userid:[email protected]:port
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文