簡體   English   中英

Python request.get引發HTTPS網址的ConnectionError

[英]Python requests.get raises ConnectionError for HTTPS url

我正在嘗試這個簡單的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。 希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM