![](/img/trans.png)
[英]Python - How to handle HTTPS request with (Urllib2 + SSL) though a HTTP proxy
[英]Python urllib2 > HTTP Proxy > HTTPS request
這項工作很好:
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()
但是,如果http更改為https :
...
print urllib2.urlopen('https://www.google.com').read()
有錯誤:
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]
為什么以及如何解決這個問題?
改變這一行:
urllib2.ProxyHandler({'http': 'http://user:pass@proxy:3128'}))
對此:
urllib2.ProxyHandler({'https': 'http://user:pass@proxy:3128'}))
這對我來說可以。
urllib2的文檔說明如下:
注意:目前urllib2不支持通過代理獲取https位置。 但是,可以通過擴展urllib2來啟用此功能, 如此配方中所示。
我必須承認上面的配方不能立即用於Jython 2.5.3,但我還在努力。
更新 :我將此補丁應用於Jython 2.5.3,它對我有用。 我現在可以通過代理服務器獲取HTTPS資源。
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()
在Windows上,errno 10060是一個winsock錯誤,意味着連接超時。 您是否可以使用代理設置為http:// user:pass @ proxy:3128的網絡瀏覽器從同一台計算機訪問https://www.google.com ? 您確定您的代理服務器可以在同一端口上同時處理https和http嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.