我试图使用python从web获取数据。我导入了urllib。请求包,但在执行时,我得到错误:

certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)

我在Mac OS High Sierra上使用Python 3.7。 我试图从CSV文件: https://s3.amazonaws.com/assets.datacamp.com/production/course_1606/datasets/winequality-red.csv

当我将URL更改为“http”时-我能够获得数据。但是,我认为这避免了检查SSL证书。

所以我在网上找到了一个解决方案: 运行/Applications/Python\ 3.7/Install\ certificates .命令

这解决了我的问题。但是我没有SSL之类的知识。你能帮我理解一下它到底是怎么解决我的问题的吗?

如果可能的话,请给我推荐一些好的资源来了解安全与证书。我是新手。

谢谢!

注意:我确实通过链接openssl, python请求错误:“证书验证失败”

我的问题与链接中的问题不同,因为我想知道当我安装certifi包或运行install \ Certificates.command来修复错误时实际发生了什么。我对证券的理解很差。


当前回答

我在Ventura上用python 3.9-10遇到了这个问题,尽管我已经尝试过了:

将我的私有CA证书添加到/etc/ssl/cert.pem, /etc/ssl/certs/ 将我的私有CA证书添加到特定证书的cert.pem文件中 将我的私人CA证书添加到我的钥匙链到“系统”桶 设置REQUESTS_CA_BUNDLE = / etc / ssl / cert.pem

这使请求工作,但httpconnection和urllib3验证失败,所以它仍然有一个地方添加CA证书:

/usr/local/etc/ca-certificates / cert.pem

我相信这是因为我已经通过brew安装了openssl,这设置了上面的文件,并从/usr/local/etc/openssl@1.1/cert.pem中添加了符号链接。

因此,如果有人在通过brew安装openssl后遇到证书验证失败的情况,那么这可能就是原因所在。

其他回答

警告:我对证书不是很了解,但我认为这值得尽早检查。

在花时间重新配置代码/包/系统之前,请确保您试图下载的服务器没有问题。

我认为这个错误可能会产生误导,因为“无法获得本地发行者证书”看起来好像是本地机器出了问题,但事实未必如此。

尝试将您正在尝试加载的页面更改为可能不错的页面,例如https://www.google.com,然后查看问题是否仍然存在。此外,使用https://www.digicert.com/help/上的搜索工具检查出问题的域名。

在我的例子中,DigiCert的工具告诉我“证书不是由受信任的权威签署的(根据Mozilla的根存储进行检查)。”这就解释了为什么我似乎已经安装了根证书,但仍然出现错误。当我测试用HTTPS加载一个不同的网站时,我没有遇到任何问题。

如果这种情况适用于你,那么我认为你可能有3个逻辑选项(按优先级排序):1)修复服务器(如果它在你的控制之下),2)禁用证书检查,同时继续使用HTTPS, 3)跳过HTTPS转到HTTP。

Certifi提供了Mozilla精心策划的根证书集合,用于验证SSL证书的可信度,同时验证TLS主机的身份。它已经从Requests项目中提取出来。

pip install certifi

或者运行下面的程序代码:

# 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()

Brew没有运行Mac Python3包中的Install certificates .命令。

对我来说,所有建议的解决办法都不管用。但是,我是在公司PC的终端上运行代码的,PC上安装了一个名为ZScaler的IT安全软件包。禁用ZScaler软件解决了我所有的问题。

我在Ventura上用python 3.9-10遇到了这个问题,尽管我已经尝试过了:

将我的私有CA证书添加到/etc/ssl/cert.pem, /etc/ssl/certs/ 将我的私有CA证书添加到特定证书的cert.pem文件中 将我的私人CA证书添加到我的钥匙链到“系统”桶 设置REQUESTS_CA_BUNDLE = / etc / ssl / cert.pem

这使请求工作,但httpconnection和urllib3验证失败,所以它仍然有一个地方添加CA证书:

/usr/local/etc/ca-certificates / cert.pem

我相信这是因为我已经通过brew安装了openssl,这设置了上面的文件,并从/usr/local/etc/openssl@1.1/cert.pem中添加了符号链接。

因此,如果有人在通过brew安装openssl后遇到证书验证失败的情况,那么这可能就是原因所在。

我愿意提供一份参考资料。我使用cmd +空格,然后键入Install Certificates.command,然后按Enter。过了一会儿,弹出命令行界面开始安装。

 -- removing any existing file or link
 -- creating symlink to certifi certificate bundle
 -- setting permissions
 -- update complete

最后,它修复了错误。