我试图使用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来修复错误时实际发生了什么。我对证券的理解很差。
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 .命令。
对我来说,问题是我在我的.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
此页是谷歌中“证书验证失败:无法获得本地颁发者证书”的最高点击率,因此,虽然这没有直接回答最初的问题,但下面是对具有相同症状的问题的修复。我在尝试将TLS添加到xmlrpc服务时遇到了这个问题。这需要使用相当低级的ssl。SSLContext类。该错误表示缺少证书。修复方法是在构造SSLContext对象时做几件事:
首先,在客户端:
def get_client():
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
# Load the default certs:
context.load_default_certs()
# Optionally, install the intermediate certs.
# This _should_ be handled by the server, but
# maybe helpful in some cases.
# context.load_verify_locations('path/to/ca_bundle.crt')
return xmlrpc.client.ServerProxy('https://server.yourdomain.com/', context=context)
在服务器端,你需要在上下文中安装中间的certs:
class SecureXMLRPCServer(socketserver.TCPServer,
xmlrpc.server.SimpleXMLRPCDispatcher):
# https://gist.github.com/monstermunchkin/1100226
allow_reuse_address = True
def __init__(self, addr, certfile, keyfile=None,
ca_bundle=None,
requestHandler=xmlrpc.server.SimpleXMLRPCRequestHandler,
logRequests=True, allow_none=False, encoding=None,
bind_and_activate=True, ssl_version=ssl.PROTOCOL_TLSv1_2):
self.logRequests = logRequests
# create an SSL context
self.context = ssl.SSLContext(ssl_version)
self.context.load_default_certs()
# The server is the correct place to load the intermediate CA certificates:
self.context.load_verify_locations(ca_bundle)
self.context.load_cert_chain(certfile=certfile, keyfile=keyfile)
xmlrpc.server.SimpleXMLRPCDispatcher.__init__(self, allow_none,
encoding)
# call TCPServer constructor
socketserver.TCPServer.__init__(self, addr, requestHandler,
bind_and_activate)
if fcntl is not None and hasattr(fcntl, 'FD_CLOEXEC'):
flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD)
flags |= fcntl.FD_CLOEXEC
fcntl.fcntl(self.fileno(), fcntl.F_SETFD, flags)
def get_request(self):
newsocket, fromaddr = self.socket.accept()
# create an server-side SSL socket
sslsocket = self.context.wrap_socket(newsocket, server_side=True)
return sslsocket, fromaddr