我正在尝试这个简单的python 2.7代码:
import requests
response = requests.get(url="https://sslbl.abuse.ch", verify=False)
print response
我正在使用
verify=False
来忽略验证SSL证书。我收到以下异常:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='sslbl.abuse.ch', port=443): Max retries exceeded with url: / (Caused by <class 'socket.error'>: [Errno 10054] An existing connection was forcibly closed by the remote host
如果我尝试另一个https网址(例如twitter.com),一切正常。
可能是什么问题?如何像浏览器一样获得响应?
更新:
升级请求版本后,我得到相同的
ConnectionError
,但添加了一些警告:C:\Python27\lib\site-packages\requests\packages\urllib3\util\ssl_.py:315: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
C:\Python27\lib\site-packages\requests\packages\urllib3\util\ssl_.py:120: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
最佳答案
我没有使用Python 2.7来执行任务,但是我尝试打开python3.2提供的URL(我认为它应该适用于所有Python3x)。没有例外。这就是我所做的(>>>被省略):
from urllib.request import urlopen
url = "https://sslbl.abuse.ch"
response = urlopen(url)
type(response)
<class 'http.client.HTTPResponse'>
在Python文档中,查看此输出:
i = 0
with open(url) as response:
for line in response:
line = line.decode('utf-8')
if "Show more information about this SSL certificate" in line:
i += 1
print(i)
1060
我建议使用Python3x。希望这可以帮助!