我试图使用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来修复错误时实际发生了什么。我对证券的理解很差。


当前回答

由于这个问题没有[macos]标签,我在ubuntu下发布了一个同样问题的解决方案:

sudo apt install ca-certificates
sudo update-ca-certificates --fresh
export SSL_CERT_DIR=/etc/ssl/certs

解决方案来自Github上的Zomatree。

其他回答

对于那些这个问题仍然存在的人:- MacOS上的Python 3.6(还有其他版本吗?)附带了自己的私有OpenSSL副本。这意味着系统中的信任证书不再被Python ssl模块用作默认值。要解决这个问题,您需要在系统中安装一个证书包。

你可以尝试两种方法:

1)通过PIP:

pip install --upgrade certifi

2)如果它不起作用,尝试运行Python 3.6 for Mac附带的certificates .command:

open /Applications/Python\ 3.6/Install\ Certificates.command

无论如何,您现在应该已经安装了证书,并且Python应该能够通过HTTPS进行连接而不会出现任何问题。

希望这对你有所帮助。

突然,我开始在windows环境中遇到这个问题。更糟糕的是,当我运行pip时也出现了这个问题,所以问题不是与远程服务器证书有关。

在尝试了许多不同的方法后,我从多个答案中找到了解决方案:

在pip.ini中添加可信主机:pip config set global。“pypi.python.org”(不工作,只传递PIP安装参数) 更新系统证书:pip install pip-system-certs(在安装python- certificate -win32时无效)

现在https请求再次工作\o/

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 .命令。

在开始时粘贴以下代码:

# paste this at the start of code
import ssl 

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

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