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


当前回答

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

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

最后,它修复了错误。

其他回答

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

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

最后,它修复了错误。

对于那些这个问题仍然存在的人:- 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进行连接而不会出现任何问题。

希望这对你有所帮助。

在开始时粘贴以下代码:

# 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

我在OSX上遇到了同样的问题,而我的代码在Linux上完全没问题,你在你的问题中给出了答案!

在检查您指向/Applications/Python 3.7/Install certificates .command的文件后,发现该命令将默认Python安装的根证书替换为通过certifi包提供的根证书。

Certifi是根证书的集合。每个SSL证书都依赖于一个信任链:您信任一个特定的证书,因为您信任该证书的父证书,因此您信任该证书的父证书,等等。在某些情况下,没有“父”证书,这些是“根”证书。对于这些问题,除了捆绑普遍信任的根证书(通常是大型信托公司,如eg。“DigiCert”)。

例如,您可以在浏览器安全设置中看到根证书(例如Firefox->首选项->隐私和安全->视图证书->权威)。

回到最初的问题,在运行.command文件之前,在干净的安装上执行这个命令会返回一个空列表:

import os
import ssl                                        
openssl_dir, openssl_cafile = os.path.split(      
    ssl.get_default_verify_paths().openssl_cafile)
# no content in this folder
os.listdir(openssl_dir)
# non existent file
print(os.path.exists(os.path.join(openssl_dir, openssl_cafile)))

这意味着OSX上的Python安装没有默认的证书颁发机构。可能的默认值正是证书包提供的。

在此之后,您只需创建一个具有正确默认值的SSL上下文,如下所示(certificate .where()给出了证书颁发机构的位置):

import platform
# ...

ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.check_hostname = True
ssl_context.load_default_certs()

if platform.system().lower() == 'darwin':
    import certifi
    ssl_context.load_verify_locations(
        cafile=os.path.relpath(certifi.where()),
        capath=None,
        cadata=None)

然后像这样从python请求一个url:

import urllib
# previous context
https_handler = urllib.request.HTTPSHandler(context=ssl_context)

opener = urllib.request.build_opener(https_handler)
ret = opener.open(url, timeout=2)

对我来说,问题是我在我的.bash_profile中设置了REQUESTS_CA_BUNDLE

/Users/westonagreene/.bash_profile:
...
export REQUESTS_CA_BUNDLE=/usr/local/etc/openssl/cert.pem
...

一旦我将REQUESTS_CA_BUNDLE设置为空白(即从.bash_profile中删除),请求就会再次工作。

export REQUESTS_CA_BUNDLE=""

该问题仅在通过CLI(命令行接口)执行python请求时出现。如果我运行请求。get(URL, CERT)它解决得很好。

Mac OS Catalina(10.15.6)。 3.6.11的Pyenv。 我正在获得的错误消息:[SSL: CERTIFICATE_VERIFY_FAILED]证书验证失败:无法获得本地颁发者证书(_ssl.c:1056)

这个答案在其他地方:https://stackoverflow.com/a/64152045/4420657