我正在编写一个简单的脚本,涉及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

当前回答

要使用的CA文件的名称可以通过验证:

cafile = 'cacert.pem' # http://curl.haxx.se/ca/cacert.pem
r = requests.get(url, verify=cafile)

如果使用verify=True,则requests使用自己的CA集,该CA集可能没有签署您的服务器证书的CA。

其他回答

正如@Rafael Almeida所提到的,您遇到的问题是由不受信任的SSL证书引起的。在我的例子中,我的服务器不信任SSL证书。为了在不影响安全性的情况下解决这个问题,我下载了证书,并将其安装到服务器上(只需双击.crt文件,然后安装证书…)。

如果你不关心证书,使用verify=False即可。

import requests

url = "Write your url here"

returnResponse = requests.get(url, verify=False)

我想参加派对太晚了,但我想把这个补丁粘贴给像我这样的流浪者!因此,下面的代码在Python 3.7.x上对我有效

在终端中输入以下内容

pip install --upgrade certifi      # hold your breath..

试着再次运行你的脚本/请求,看看它是否有效(我肯定它还不会被修复!)。如果它不起作用,那么尝试直接在终端中运行以下命令

open /Applications/Python\ 3.6/Install\ Certificates.command  # please replace 3.6 here with your suitable python version

经过几个小时的调试后,我只能使用以下包来工作:

requests[security]==2.7.0  # not 2.18.1
cryptography==1.9  # not 2.0

使用OpenSSL 1.0.2g 2016年3月1日

如果没有这些包,verify=False将无法工作。

我希望这能帮助到一些人。

要使用的CA文件的名称可以通过验证:

cafile = 'cacert.pem' # http://curl.haxx.se/ca/cacert.pem
r = requests.get(url, verify=cafile)

如果使用verify=True,则requests使用自己的CA集,该CA集可能没有签署您的服务器证书的CA。