Python请求获取SSLerror

时间:2015-02-23 05:58:39

标签: python ssl ssl-certificate python-requests

尝试使用Requests会话发出简单的get请求,但我一直在为特定站点获取SSLerror。我想也许问题出在网站上(我使用https://www.ssllabs.com进行了扫描,结果如下),但我无法确定,因为我对此领域一无所知:)我肯定想了解什么是继续。

解决方案/解释会很棒, 谢谢!

代码:

import requests

requests.get('https://www.reporo.com/')

我收到了下一个错误:

SSLError: [Errno bad handshake] [('SSL routines', 'SSL3_GET_SERVER_CERTIFICATE', 'certificate verify failed')]

---------------------------------------------------------------------------
SSLError                                  Traceback (most recent call last)
<ipython-input-7-cfc21b287fee> in <module>()
----> 1 requests.get('https://www.reporo.com/')

/usr/local/lib/python2.7/dist-packages/requests/api.pyc in get(url, **kwargs)
     63 
     64     kwargs.setdefault('allow_redirects', True)
---> 65     return request('get', url, **kwargs)
     66 
     67 

/usr/local/lib/python2.7/dist-packages/requests/api.pyc in request(method, url, **kwargs)
     47 
     48     session = sessions.Session()
---> 49     response = session.request(method=method, url=url, **kwargs)
     50     # By explicitly closing the session, we avoid leaving sockets open which
     51     # can trigger a ResourceWarning in some cases, and look like a memory leak

/usr/local/lib/python2.7/dist-packages/requests/sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    459         }
    460         send_kwargs.update(settings)
--> 461         resp = self.send(prep, **send_kwargs)
    462 
    463         return resp

/usr/local/lib/python2.7/dist-packages/requests/sessions.pyc in send(self, request, **kwargs)
    571 
    572         # Send the request
--> 573         r = adapter.send(request, **kwargs)
    574 
    575         # Total elapsed time of the request (approximately)

/usr/local/lib/python2.7/dist-packages/requests/adapters.pyc in send(self, request, stream, timeout, verify, cert, proxies)
    429         except (_SSLError, _HTTPError) as e:
    430             if isinstance(e, _SSLError):
--> 431                 raise SSLError(e, request=request)
    432             elif isinstance(e, ReadTimeoutError):
    433                 raise ReadTimeout(e, request=request)

SSLError: [Errno bad handshake] [('SSL routines', 'SSL3_GET_SERVER_CERTIFICATE', 'certificate verify failed')]

我在https://www.ssllabs.com进行扫描并得到以下信息:

SSL Report: reporo.com
Assessed on:  Sun Feb 22 21:42:57 PST 2015 | Clear cache Scan Another >>

    Server  Domain(s)   Test time   Grade
1   154.51.128.13 
Certificate not valid for domain name 
reporo.com
Sun Feb 22 21:40:53 PST 2015 
Duration: 9.167 sec -
2   198.12.15.168 
protected.ddosdefend.com 
Ready 
www.reporo.com

Sun Feb 22 21:41:02 PST 2015 
Duration: 115.189 sec   
F

4 个答案:

答案 0 :(得分:20)

www.reporo.com(不是reporo.com)的证书本身有效,但缺少ssllabs报告中显示的连锁证书:

Chain issues    Incomplete
....
2   Extra download  Thawte DV SSL CA 
Fingerprint: 3ca958f3e7d6837e1c1acf8b0f6a2e6d487d6762 

&#34; Incomplete&#34;和&#34;额外下载&#34;是主要观点。有些浏览器会丢失丢失的链证书,其他浏览器会进行下载,其他浏览器会失败。如果您使用新的Firefox配置文件(没有缓存任何证书)尝试该站点,它也将失败。

您可以下载缺失的链证书,并将其作为可信CA证书,并使用verify参数进行请求。不要只是禁用验证,因为那样你就可以接受中间人攻击了。

分步说明:

答案 1 :(得分:5)

您可以停用证书验证:

requests.get('https://www.reporo.com/', verify=False)

但没有证书验证,则没有man-in-the-middle attack保护。

答案 2 :(得分:3)

我有同样的错误。将请求-2.17.3的请求降级到请求-2.11.0为我解决了这个问题。

pip uninstall requests
pip install requests==2.11.0

答案 3 :(得分:1)

当网站具有由浏览器确定的有效证书但服务器未提供完整的证书链时,Steffen Ullrich 给出了最佳答案。

有时只是缺少根证书或中间证书,浏览器可以即时下载或验证缺少的证书,但是使用请求模块,您将需要手动验证它。我使用 SSL labs 将完整的证书链确定为 .pem 文件。输入感兴趣的网址,等待测试完成。然后导航到并展开“认证路径”。可能有多个可信路径(或没有,在这种情况下最终请求将不会成功),在右侧您会看到一个下载链按钮。

将整个链复制为文本并将其另存为 .pem 文件。然后在发出请求时将这个 .pem 文件的路径传递给 requests 函数:

r = requests.get(url, verify= "path/to/chain.pem")