Python请求抛出SSL错误

Gid*_* Av 6 python macos ssl python-requests

这是使用python请求的SSLError的后续内容:

我刚安装requests在Mac OSX 10.8.5上.我requests.get在失踪证书上的第一次尝试失败了:

SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

  • 上面的线程说要查找,/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/re??quests/cacert.pem但实际上我甚至没有.../site-packages/requests目录.我不清楚这是否应该由安装添加(我使用过pip)

  • 进一步的线程和requests文档说安装certifi,所以我做了.但是现在我得到了一个不同的错误:

    python -c 'import requests; requests.get("https://api.github.com/events")'    /usr/lib/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:90: 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.
      InsecurePlatformWarning
    Traceback (most recent call last):
    ...
      File "/usr/lib/anaconda/lib/python2.7/site-packages/requests/adapters.py", line 431, in send
        raise SSLError(e, request=request)
    requests.exceptions.SSLError: [Errno 1] _ssl.c:504: error:0D0890A1:asn1 encoding routines:ASN1_verify:unknown message digest algorithm
    
    Run Code Online (Sandbox Code Playgroud)

谢谢!

boa*_*ter 8

请注意,您正在使用HTTPS.如" 请求手册"中所述

要检查主机的SSL证书,可以使用verify参数[...]默认情况下,verify设置为True

以下是解决这个问题的几种方法:

更新OpenSSL(可能会解决您的问题)

取自这里:

如果遇到以下错误之一:

error:0D0890A1:asn1 encoding routines:ASN1_verify:unknown message digest algorithm
error:0D0C50A1:asn1 encoding routines:ASN1_item_verify:unknown message digest algorithm
The software you are using might be compiled with a version too old of OpenSSL that does not take certificates signed with sha256WithRSAEncryption into account.
Run Code Online (Sandbox Code Playgroud)

它至少需要OpenSSL 0.9.8o来管理SHA256.OpenSSl 0.9.7m仅确保部分管理,仅适用于服务器模式.

检查您的openssl版本

openssl version
OpenSSL 1.0.1k-fips 8 Jan 2015
Run Code Online (Sandbox Code Playgroud)

如果您的版本小于OpenSSL0.9.8o,则必须更新其版本(OS X):

brew update
brew install openssl
brew link --force openssl
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请尝试这种方式:

brew uninstall openssl
rm -rf /usr/local/openssl
brew install openssl
Run Code Online (Sandbox Code Playgroud)
  • openssl安装之前有一个问题,OS X 10.10.3并重新安装它修复它
  • 这些命令行将卸载openssl,从硬盘中删除其文件夹并重新安装(更新版本)

安装 certifi

取自这里

默认情况下,Requests捆绑一组它信任的根CA,这些CA来自Mozilla信任库.但是,这些仅针对每个请求版本更新一次.这意味着如果您固定一个请求版本,您的证书可能会变得非常过时.

从请求版本2.4.0开始,如果系统上存在证书,请求将尝试使用来自certifi的证书.这允许用户更新其受信任的证书,而无需更改在其系统上运行的代码.

为安全起见,我们建议经常升级certifi!

换句话说,certifi如果您有Request 2.4.0或更新,请尝试安装:

pip install certifi
Run Code Online (Sandbox Code Playgroud)

希望这能解决问题.

使用不同版本的OpenSSL和请求

使用Google进行调查,我发现Python 2中的OpenSSL存在问题:

不过,我使用的Python 2.7.6,Requests 2.2.1OpenSSL 1.0.1f 6 Jan 2014一切正常运行.

通过证书

在其他情况下,requests.get如果主机的证书由您签名,您可能需要告知证书文件的路径.

requests.get("https://api.github.com/events", verify=True, cert=['/path/to/my/ca.crt'])
Run Code Online (Sandbox Code Playgroud)

将verify参数设置为False(NOT RECOMMENDED!)

如果您想避免证书验证,则必须传递verify=False给该request.get方法.

python -c 'import requests; requests.get("https://api.github.com/events", verify=False)'
Run Code Online (Sandbox Code Playgroud)

或从script.py文件:

import requests
res = requests.get("https://api.github.com/events", verify=False)
print res
Run Code Online (Sandbox Code Playgroud)

终奌站:

$ python script.py
<Response [200]>
Run Code Online (Sandbox Code Playgroud)

重要:非常糟糕的主意; 你可以被 MITM攻击,这是一个关键的安全漏洞.

  • 请不要在未描述负面影响(中间人攻击)的情况下建议禁用证书验证,并且仅应将其用于测试或在其他方面与安全性无关的情况下使用。 (2认同)