使用 HTTP 代理 - Python
我熟悉这样一个事实:我应该将 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>
额外信息:
urllib.urlopen(....) 工作正常!这只是 urllib2 在玩把戏...
我尝试了 @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>
有什么想法吗?
I familiar with the fact that I should set the HTTP_RPOXY environment variable to the proxy address.
Generally urllib works fine, the problem is dealing with urllib2.
>>> urllib2.urlopen("http://www.google.com").read()
returns
urllib2.URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>
or
urllib2.URLError: <urlopen error [Errno 11004] getaddrinfo failed>
Extra info:
urllib.urlopen(....) works fine! It is just urllib2 that is playing tricks...
I tried @Fenikso answer but I'm getting this error now:
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>
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
即使没有 HTTP_PROXY 环境变量,您也可以做到这一点。尝试这个示例:
在您的情况下,代理服务器似乎确实拒绝连接。
更多值得尝试的事情:
编辑 2014 年:
这似乎是一个流行的问题/答案。但是今天我将使用第三方
requests
模块。对于一个请求只需执行以下操作:
对于多个请求,请使用
Session
对象,这样您就不必在所有请求中添加proxies
参数:You can do it even without the HTTP_PROXY environment variable. Try this sample:
In your case it really seems that the proxy server is refusing the connection.
Something more to try:
Edit 2014:
This seems to be a popular question / answer. However today I would use third party
requests
module instead.For one request just do:
For multiple requests use
Session
object so you do not have to addproxies
parameter in all your requests:我建议您只使用 requests 模块。
它比内置的 http 客户端要容易得多:
http://docs.python-requests.org/en/latest/index.html
示例用法:
I recommend you just use the requests module.
It is much easier than the built in http clients:
http://docs.python-requests.org/en/latest/index.html
Sample usage:
只是想提一下,您可能还需要设置
https_proxy
操作系统环境变量,以防需要访问 https URL。就我而言,这对我来说并不明显,我花了几个小时才发现这一点。
我的用例:Win 7,jython-standalone-2.5.3.jar,通过 ez_setup.py 安装 setuptools
Just wanted to mention, that you also may have to set the
https_proxy
OS environment variable in case https URLs need to be accessed.In my case it was not obvious to me and I tried for hours to discover this.
My use case: Win 7, jython-standalone-2.5.3.jar, setuptools installation via ez_setup.py
Python 3:
Python 3:
我在 jython 客户端上遇到了这个。
服务器仅使用 TLS 进行通信,而客户端则使用 SSL 上下文。
javax.net.ssl.SSLContext.getInstance("SSL")
一旦客户端使用 TLS,一切就开始工作了。
I encountered this on jython client.
The server was only talking TLS and the client using SSL context.
javax.net.ssl.SSLContext.getInstance("SSL")
Once the client was to TLS, things started working.