因为mac 现在主要用 LibreSSL,openssl的更新没有及时。
如果需要更新,可以用brew来安装 openssl,并将其设置为 系统默认的openssl。
cooldeMacBook-Pro:~ cool$ brew install openssl Updating Homebrew... ==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2p.el_capitan.bottle.tar. ######################################################################## 100.0% ==> Pouring openssl-1.0.2p.el_capitan.bottle.tar.gz ==> Caveats A CA file has been bootstrapped using certificates from the SystemRoots keychain. To add additional certificates (e.g. the certificates added in the System keychain), place .pem files in /usr/local/etc/openssl/certs and run /usr/local/opt/openssl/bin/c_rehash openssl is keg-only, which means it was not symlinked into /usr/local, because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries. If you need to have openssl first in your PATH run: echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile For compilers to find openssl you may need to set: export LDFLAGS="-L/usr/local/opt/openssl/lib" export CPPFLAGS="-I/usr/local/opt/openssl/include" ==> Summary ? /usr/local/Cellar/openssl/1.0.2p: 1,793 files, 12.2MB cooldeMacBook-Pro:~ cool$ openssl version OpenSSL 0.9.8zh 14 Jan 2016 cooldeMacBook-Pro:~ cool$
调用方法一:更新系统环境变量:
cooldeMacBook-Pro:~ cool$ echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile cooldeMacBook-Pro:~ cool$ source ~/.bash_profile cooldeMacBook-Pro:~ cool$ openssl version OpenSSL 1.0.2p 14 Aug 2018
调用方法二:
1、删除了.bash_profile中 的用户配置路径 openssl,
2、找到系统 /usr/bin/openssl
重命名乱连接,做备份 /usr/bin/openssl.bak
3、查看 新版openssl的安装目录:
cooldeMacBook-Pro:~ cool$ ls -l /usr/local/opt/openssl lrwxr-xr-x 1 cool admin 24 Oct 29 21:06 /usr/local/opt/openssl -> ../Cellar/openssl/1.0.2p
4、备份原来的/usr/bin/openssl
,并重新设置系统openssl的软连接:
cooldeMacBook-Pro:~ cool$ sudo ln -s /usr/local/Cellar/openssl/1.0.2p/bin/openssl /usr/bin/openssl Password: cooldeMacBook-Pro:~ cool$
关于python中的 ssl版本问题:
urllib
是Python自带的标准库,无需安装,直接可以用。而urllib 是python自带的标准库,里面有ssl模块。也就是说python自带的的ssl模块与系统的openssl软件是无关的,版本号当然可以不相同。
cooldeMacBook-Pro:~ cool$ python -c "import ssl; print(ssl.OPENSSL_VERSION)" OpenSSL 0.9.8zh 14 Jan 2016 cooldeMacBook-Pro:~ cool$ python Python 2.7.10 (default, Oct 23 2015, 19:19:21) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import ssl >>> ssl.OPENSSL_VERSION 'OpenSSL 0.9.8zh 14 Jan 2016' >>>
所以使用 旧版本的 urllib 库的 爬虫只支持ssl 1.0协议,导致ssl高版本协议的服务器拒绝与其连接。
HTTP exception InvalidCertificateException (Host codeload.github.com returned an invalid certificate ([SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:548))) downloading https://codeload.github.com/seanliang/ConvertToUTF8/zip/1.2.11.