【问题标题】:Python requests.get raises ConnectionError for HTTPS urlPython requests.get 引发 HTTPS url 的 ConnectionError
【发布时间】:2016-04-08 07:20:20
【问题描述】:

我正在尝试这个简单的 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.

【问题讨论】:

  • 什么版本的请求?
  • 为什么不在 Python3x 中使用 urllib.request 呢?请在下面查看我建议的答案。谢谢。

标签: python ssl https python-requests


【解决方案1】:

我没有使用 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。希望这会有所帮助!

【讨论】:

  • 感谢您的帮助。由于工作地点要求,我必须使用 python 27。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
相关资源
最近更新 更多