我需要在公司内部网上使用curtom root证书,并在Mac OS中加载它们TrustStore(KeyChain)确实解决了所有浏览器和GUI应用程序的问题。
看起来它甚至可以与Mac OS X附带的curl
版本一起工作,但它不适用于python,即使Mac OS 10.12 Sierra附带的版本(Python 2.7.10)
不过,似乎我会受到以下打击:
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>
因为我在很多Python工具中遇到这个问题,如果我找到一种方法来避免它而不必修补它,我将非常感激。
自己提供自定义CA证书不是一种选择,因为我无法修补我使用的数十种Python工具。
大多数工具都使用requests
库,但有一些工具直接在Python中使用本机ssl支持。
这也是使用MacOS Sierrra的Python 3.6中的一个问题。我知道你的用例不同。但在调查这个问题时,我偶然发现了这个问题。所以如果有人也有这篇文章值得一试:
http://www.cdotson.com/2017/01/sslerror-with-python-3-6-x-on-macos-sierra/
简而言之:Python 3.6不再依赖于MacOS的openSSL。它附带了自己的openSSL捆绑,无法访问MacOS的根证书。
您有两种选择:
运行Python 3.6附带的安装命令
cd /Applications/Python\ 3.6/
./Install\ Certificates.command
要么
pip install certifi
我选择了第一个选项并且它有效。
如果将其他证书放在PEM包文件中,则可以使用这两个环境变量来覆盖Python openssl和请求使用的默认证书库。
SSL_CERT_FILE=/System/Library/OpenSSL/cert.pem
REQUESTS_CA_BUNDLE=/System/Library/OpenSSL/cert.pem
请注意,此文件不存在,您需要自己构建它。
作为更新和数据点,我遇到了在macOS 10.13.4上运行Python 3.7.0的这个问题:
$ ipython
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import bokeh.sampledata
In [2]: bokeh.sampledata.download()
Using data directory: /Users/me/.bokeh/data
...
SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)
解决问题的说明在/Applications/Python\ 3.7/ReadMe.rtf
根据建议,运行/Applications/Python\ 3.7/Install\ Certificates.command
解决了这个问题:
从终端:
$ /Applications/Python\ 3.7/Install\ Certificates.command
重启IPython ......
$ ipython
>>> import bokeh.sampledata
>>> bokeh.sampledata.download()
Using data directory: /Users/me/.bokeh/data
Downloading: CGM.csv (1589982 bytes)
1589982 [100.00%]
...
对我来说,/Applications/Python\ 3.6/./Install\ Certificates
命令在pip certifi安装上失败了。我在mac High Sierra上使用python3所以pip有点失败,我不得不使用pip3。
所以我在这做了什么:
pip3 install --update certify
请注意,您最终将获得cert.pem符号链接:/Library/Frameworks/Python.framework/Versions/3.6/etc/openssl/
Mac brew安装python env。
$ python3
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import certifi
>>> certifi.where()
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/certifi/cacert.pem'
>>>
或者从命令行:
$ python -m certifi
然后需要链接cacert.pem作为cert.pem
$ ln -s /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/certifi/cacert.pem cert.pem
$ pwd
/Library/Frameworks/Python.framework/Versions/3.7/etc/openssl
rehash
然后工作正常。