从公司防火墙后面使用urllib2打开网站-11004 getaddrinfo失败 [英] opening websites using urllib2 from behind corporate firewall - 11004 getaddrinfo failed

查看:103
本文介绍了从公司防火墙后面使用urllib2打开网站-11004 getaddrinfo失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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')

遇到错误

URLError: <urlopen error [Errno 11004] getaddrinfo failed>

我尝试使用不同的处理程序(尝试ProxyHandler的方式也略有不同),但似乎不起作用.

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?

推荐答案

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

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用于为您要访问的站点而不是通过代理提供凭据.上面的代码片段可能会对您有所帮助.

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.

这篇关于从公司防火墙后面使用urllib2打开网站-11004 getaddrinfo失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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