我熟悉这样一个事实:我应该将HTTP-RPOXY环境变量设置为代理地址。
通常urllib工作正常,问题是处理urllib2。
>>> urllib2.urlopen("http://www.google.com").read()
回报
urllib2.URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>
或者
urllib2.URLError: <urlopen error [Errno 11004] getaddrinfo failed>
我试过@Fenikso答案,但现在我得到了这个错误:
URLError: <urlopen error [Errno 10060] A connection attempt failed because the
connected party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond>
有什么想法吗?
只是想提一下,如果需要访问https url,您可能还必须设置
https_proxy
OS环境变量。 在我的情况下,这对我来说并不明显,我花了几个小时试图发现这一点。我的用例:Win 7,jython-standalone-2.5.3.jar,通过ez_setup.py安装setuptools
即使不使用HTTP_PROXY环境变量,也可以执行此操作。尝试此示例:
在您的情况下,代理服务器似乎确实在拒绝连接。
还有更多的尝试:
编辑2014: 这似乎是一个很受欢迎的问题/答案。不过,今天我将使用第三方^{} 模块。
对于一个请求,请执行以下操作:
对于多个请求,请使用
Session
对象,这样就不必在所有请求中添加proxies
参数:我建议您使用“请求”模块。
它比内置的http客户端容易得多: http://docs.python-requests.org/en/latest/index.html
示例用法:
相关问题 更多 >
编程相关推荐