Brew 3.6.1的Brew安装:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败

Jor*_*tao 15 python ssl

我安装了python 3.6

brew install python3

并尝试six.moves.urllib.request.urlretrieve从https 下载文件,但它会引发错误

ssl.SSLError:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:749)

在Python安装(从.pkg)的,自述指示一个需要运行Install Certificates.command在安装后

  1. 安装 certifi
  2. 符号链接路径的证书certify路径

能够使用证书.

但是,在brew安装中,此文件不存在,似乎不会运行.

Jor*_*tao 28

看来,由于某种原因,Brew还没有运行Install Certificates.commandMac3的Python3包中的那个.此问题的解决方案是在以下脚本Install Certificates.command之后运行以下脚本(从中复制)brew install python3:

# install_certifi.py
#
# sample script to install or update a set of default Root Certificates
# for the ssl module.  Uses the certificates provided by the certifi package:
#       https://pypi.python.org/pypi/certifi

import os
import os.path
import ssl
import stat
import subprocess
import sys

STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
             | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
             | stat.S_IROTH |                stat.S_IXOTH )


def main():
    openssl_dir, openssl_cafile = os.path.split(
        ssl.get_default_verify_paths().openssl_cafile)

    print(" -- pip install --upgrade certifi")
    subprocess.check_call([sys.executable,
        "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])

    import certifi

    # change working directory to the default SSL directory
    os.chdir(openssl_dir)
    relpath_to_certifi_cafile = os.path.relpath(certifi.where())
    print(" -- removing any existing file or link")
    try:
        os.remove(openssl_cafile)
    except FileNotFoundError:
        pass
    print(" -- creating symlink to certifi certificate bundle")
    os.symlink(relpath_to_certifi_cafile, openssl_cafile)
    print(" -- setting permissions")
    os.chmod(openssl_cafile, STAT_0o775)
    print(" -- update complete")

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

  • 我必须添加`import ssl; ssl._create_default_https_context = ssl._create_stdlib_context` 添加到我的代码中以使其正常工作。 (6认同)
  • 我运行了上面的命令,它似乎可以成功运行,但是仍然出现错误。关于如何调试的任何想法? (3认同)
  • 作为参考,这个脚本的来源在这里:https://github.com/python/cpython/blob/master/Mac/BuildScript/resources/install_certificates.command (3认同)

Ken*_*Ken 16

  • 找出默认的cafile:
python -c 'import ssl; print(ssl.get_default_verify_paths().openssl_cafile)'
Run Code Online (Sandbox Code Playgroud)

/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/etc/ssl/cert.pem

sudo mkdir -p /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/etc/ssl/certs
Run Code Online (Sandbox Code Playgroud)
  • 找出ca文件 certifi
python -c 'import certifi; print(certifi.where())'
Run Code Online (Sandbox Code Playgroud)

'/usr/local/lib/python3.7/site-packages/certifi/cacert.pem'

  • 复制到
sudo cp /usr/local/lib/python3.7/site-packages/certifi/cacert.pem
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/etc/ssl/certs/cert.pem
Run Code Online (Sandbox Code Playgroud)


Cla*_*MBE 9

我的Mac OS X解决方案:

1)使用从官方Python语言网站https://www.python.org/downloads/下载的本机应用程序Python安装程序升级到Python 3.6.5。

我发现该安装程序比新版自制程序更好地为新Python更新了链接和符号链接。

2)使用刷新的Python 3.6目录中的“ ./Install Certificates.command”安装新证书

cd“ / Applications / Python 3.6 /” sudo“ ./Install Certificates.command”

  • @talha06 - 为了了解 Python 在计算机上的位置,请打开 python shell,输入 `python` 或 `python3`,然后输入 `import sys` 和 `print('\n'.join(sys.path))` 或者您可以在终端中键入此命令 `python -c "import sys; print('\n'.join(sys.path))"` (2认同)

小智 7

暂时,以下将禁用 ssl 检查,

import ssl
ssl._create_default_https_context = ssl._create_unverified_context
Run Code Online (Sandbox Code Playgroud)


rfe*_*rov 5

如果你需要让你的本地根证书(例如)被python信任,你可以将它添加到文件local_RootCA.crt末尾:certifi/cacert.pem

cat local_RootCA.crt >> `python -c 'import certifi; print(certifi.where())'`
Run Code Online (Sandbox Code Playgroud)

该解决方案也适用于 macos brew python 3 安装。

  • 经过一天在这次和其他 SOF 对话中尝试不同的事情后,这对我们来说是有效的。谢谢! (2认同)