我正在编写一个简单的脚本,涉及CAS、jspring安全检查、重定向等。我想使用Kenneth Reitz的python请求,因为它是一个伟大的作品!然而,CAS需要通过SSL进行验证,所以我必须先通过这一步。我不知道Python请求想要什么?这个SSL证书应该驻留在哪里?

Traceback (most recent call last):
  File "./test.py", line 24, in <module>
  response = requests.get(url1, headers=headers)
  File "build/bdist.linux-x86_64/egg/requests/api.py", line 52, in get
  File "build/bdist.linux-x86_64/egg/requests/api.py", line 40, in request
  File "build/bdist.linux-x86_64/egg/requests/sessions.py", line 209, in request 
  File "build/bdist.linux-x86_64/egg/requests/models.py", line 624, in send
  File "build/bdist.linux-x86_64/egg/requests/models.py", line 300, in _build_response
  File "build/bdist.linux-x86_64/egg/requests/models.py", line 611, in send
requests.exceptions.SSLError: [Errno 1] _ssl.c:503: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

当前回答

我在使用aws boto3时遇到了同样的问题和ssl证书验证失败的问题,通过检查boto3代码,我发现没有设置REQUESTS_CA_BUNDLE,所以我通过手动设置来修复这两个问题:

from boto3.session import Session
import os

# debian
os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(
    '/etc/ssl/certs/',
    'ca-certificates.crt')
# centos
#   'ca-bundle.crt')

对于aws-cli,我想在~/中设置REQUESTS_CA_BUNDLE。Bashrc将修复这个问题(没有测试,因为我的aws-cli没有它也能工作)。

REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt # ca-bundle.crt
export REQUESTS_CA_BUNDLE

其他回答

我找到了一个解决类似问题的具体方法。这个想法是指向存储在系统中的cacert文件,并由另一个基于ssl的应用程序使用。

在Debian中(我不确定在其他发行版中是否相同),证书文件(.pem)存储在/etc/ssl/certs/所以,这是为我工作的代码:

import requests
verify='/etc/ssl/certs/cacert.org.pem'
response = requests.get('https://lists.cacert.org', verify=verify)

为了猜测pem文件的选择,我已经浏览到url并检查哪个证书颁发机构(CA)生成了证书。

编辑:如果你不能编辑代码(因为你正在运行第三个应用程序),你可以尝试直接将pem证书添加到/usr/local/lib/python2.7/dist-packages/requests/cacert。Pem(例如将其复制到文件的末尾)。

如果你有一个依赖于请求的库,你不能修改验证路径(比如pyvmomi),那么你必须找到cacert。. pem和请求捆绑在一起,并在那里追加您的CA。下面是查找cacert的通用方法。pem位置:

窗户

C:\>python -c "import requests; print requests.certs.where()"
c:\Python27\lib\site-packages\requests-2.8.1-py2.7.egg\requests\cacert.pem

linux

#  (py2.7.5,requests 2.7.0, verify not enforced)
root@host:~/# python -c "import requests; print requests.certs.where()"
/usr/lib/python2.7/dist-packages/certifi/cacert.pem

#  (py2.7.10, verify enforced)
root@host:~/# python -c "import requests; print requests.certs.where()"
/usr/local/lib/python2.7/dist-packages/requests/cacert.pem

顺便说一句。@requests-devs,把自己的cacerts和request捆绑在一起真的很讨厌…特别是事实是,您似乎没有首先使用系统ca存储,这是没有记录在任何地方。

更新

在使用库且无法控制ca-bundle位置的情况下,你也可以显式地将ca-bundle位置设置为主机范围内的ca-bundle:

REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-bundle.crt python -c "import requests; requests.get('https://somesite.com')";

我遇到了类似或相同的认证验证问题。我读到过小于1.0.2的OpenSSL版本,请求所依赖的版本有时会在验证强证书时遇到麻烦(见这里)。CentOS 7似乎使用了1.0.1e,这似乎有问题。

我不确定如何在CentOS上解决这个问题,所以我决定允许较弱的1024bit CA证书。

import certifi # This should be already installed as a dependency of 'requests'
requests.get("https://example.com", verify=certifi.old_where())

有些服务器没有Letsencrypt的可信根证书。

例如,假设下面的url指向的服务器受到Letsencrypt SSL的保护。

requests.post(url, json=data)

此请求可能使用[SSL: CERTIFICATE_VERIFY_FAILED]失败,因为请求服务器没有Letsencrypt的根证书。

当发生这种情况时,请从下面的链接下载活动自签名的“pem”证书。

https://letsencrypt.org/certificates/。(在撰写本文时,ISRG根X1处于活动状态)

现在,在verify参数中使用它,如下所示。

requests.post(url, json=data, verify='path-to/isrgrootx1.pem')

当它说验证需要“证书路径”时,我将其指向发行者证书,以便它可以使用该证书来验证url的证书。Curl和wget可以使用该证书。但不是python请求。

我必须为python请求创建一个包含从end (leaf?)到root的所有证书的证书链,才能很好地处理它。而且这个链很自然地与cURL和Wget一起工作。

希望它能帮助别人,节省几个小时。