[英]Python urllib2 > HTTP Proxy > HTTPS request
This work fine: 这项工作很好:
import urllib2
opener = urllib2.build_opener(
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.ProxyHandler({'http': 'http://user:pass@proxy:3128'}))
urllib2.install_opener(opener)
print urllib2.urlopen('http://www.google.com').read()
But, if http change to https : 但是,如果http更改为https :
...
print urllib2.urlopen('https://www.google.com').read()
There are errors: 有错误:
Traceback (most recent call last):
File "D:\Temp\6\tmp.py", line 13, in <module>
print urllib2.urlopen('https://www.google.com').read()
File "C:\Python26\lib\urllib2.py", line 124, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python26\lib\urllib2.py", line 389, in open
response = self._open(req, data)
File "C:\Python26\lib\urllib2.py", line 407, in _open
'_open', req)
File "C:\Python26\lib\urllib2.py", line 367, in _call_chain
result = func(*args)
File "C:\Python26\lib\urllib2.py", line 1154, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "C:\Python26\lib\urllib2.py", line 1121, in do_open
raise URLError(err)
URLError: <urlopen error [Errno 10060]
Why and how solve this problem? 为什么以及如何解决这个问题?
Change this line: 改变这一行:
urllib2.ProxyHandler({'http': 'http://user:pass@proxy:3128'}))
to this: 对此:
urllib2.ProxyHandler({'https': 'http://user:pass@proxy:3128'}))
It works fine for me. 这对我来说可以。
The documentation for urllib2 says the following: urllib2的文档说明如下:
Note: Currently urllib2 does not support fetching of https locations through a proxy.
注意:目前urllib2不支持通过代理获取https位置。 However, this can be enabled by extending urllib2 as shown in this recipe .
但是,可以通过扩展urllib2来启用此功能, 如此配方中所示。
I must admit above recipe didn't work right away for Jython 2.5.3, but I'm still trying. 我必须承认上面的配方不能立即用于Jython 2.5.3,但我还在努力。
UPDATE : I applied this patch to Jython 2.5.3, and it worked for me. 更新 :我将此补丁应用于Jython 2.5.3,它对我有用。 I can fetch HTTPS resources over a proxy server now.
我现在可以通过代理服务器获取HTTPS资源。
UPDATE2 : Here is the code to query HTTPS resources with Basic authentication over HTTP Proxy (DON'T FORGET TO INSTALL PATCH FIRST (see previous update)): UPDATE2 :以下是使用HTTP代理进行基本身份验证来查询HTTPS资源的代码(不要忘记首先安装补丁(请参阅上一个更新)):
from suds.client import Client
from suds.transport.https import HttpAuthenticated
credentials = dict(username='...', password='...', proxy={'https': 'host:port', 'http': 'host:port'})
t = HttpAuthenticated(**credentials)
url = 'https://example.com/service?wsdl'
client = Client(url, transport=t)
print client.service.getFoo()
On Windows, errno 10060 is a winsock error meaning the connection timed out. 在Windows上,errno 10060是一个winsock错误,意味着连接超时。 Are you able to reach https://www.google.com from the same machine using a web browser with a proxy set to http://user:pass@proxy:3128 ?
您是否可以使用代理设置为http:// user:pass @ proxy:3128的网络浏览器从同一台计算机访问https://www.google.com ? Are you sure your proxy server can handle both https and http on the same port?
您确定您的代理服务器可以在同一端口上同时处理https和http吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.