SSL请求失败-Python OSX

SSL请求失败-Python OSX,python,python-3.x,ssl,python-requests,ssl-certificate,Python,Python 3.x,Ssl,Python Requests,Ssl Certificate,使用cookie访问经过身份验证的网页时,我收到一个SSL错误。这似乎是一个特定于mac的问题,许多人通过安装python证书来解决(正如我在下图所示的终端会话中尝试的那样),但当我尝试安装时会抛出一个异常(权限被拒绝) 我应该如何修复此错误 代码: 错误消息: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/requ

使用cookie访问经过身份验证的网页时,我收到一个SSL错误。这似乎是一个特定于mac的问题,许多人通过安装python证书来解决(正如我在下图所示的终端会话中尝试的那样),但当我尝试安装时会抛出一个异常(
权限被拒绝

我应该如何修复此错误

代码:

错误消息:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1400, in connect
    server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 814, in __init__
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 1068, in do_handshake
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Ryan/Desktop/Dev/Projects/Py/Database_intro/venv/main.py", line 97, in <module>
    f = opener.open("https://home.nest.com/home/"+url_addtl_data)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1320, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)>
编辑5/21

试图在项目中包含SSL,但引发错误。。这似乎是相关的,但是,当检查时(如上所示),您可以注意到python肯定在使用OpenSSL v。1.0.2r


应该不需要重新安装Python或pip

只需浏览到
Applications/Python 3.6
并双击
Install Certificates.command

对于python安装,
brew
,安装证书的位置可能不同

在这个环境中,证书和openssl还有其他问题/答案,以及

更新:权限被拒绝错误明确指出缺少安装权限。这个答案及其建议使用sudo和带有-H标志的sudo的注释可能会帮助您解决这个问题:

MacOS Catalina(10.15.4)。IDE:PyCharm

我也犯过类似的错误。我的第一枪是:

brew update && brew upgrade

brew uninstall --ignore-dependencies openssl; brew install https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb
请记住,我必须使用--ignoredependencies标志,因为安装的其他包依赖于OpenSSL

然后,我尝试使用以下方法重新安装Python:

brew reinstall python

最终的解决方案是删除venv,再次创建它,并安装所有依赖项。

python-c“导入ssl;打印(ssl.OPENSSL_版本)”从shell运行时的输出是什么?您可以通过手动安装(升级)pip来绕过第二个错误:。@CristiFati附加了该语句的会话和升级pip(实际上是python)的会话,但两者都没有区别。错误仍然存在。值得注意的是,我没有手动升级pip,不确定如何使用您链接的文件。既然您多次安装了python,您需要确保在命令行中调用
python
时使用的是您想要使用的。使用完整路径可能有助于解决您的问题或帮助他人帮助您解决问题。另请参见更新5/21中您尝试使用python 3运行python 2代码。尝试找出发生这种情况的原因,以及我之前的评论是否有助于解决此问题。我试图将此问题标记为重复问题,但对于具有公开悬赏的问题,似乎无法完成此操作。您链接的问题的已接受和后续答复包含在上述尝试解决方案中,减去一条很有趣但仍然没有帮助的警告。(附于问题底部)。我还问了这个问题,并悬赏,因为我认为这个问题相当紧急,其他问题都没有有效的答案。您是否尝试使用
sudo
安装证书?
Ryan-Cocuzzos-Laptop:~ Ryan$ sudo easy_install pip
Password:
Searching for pip
Best match: pip 19.1.1
Adding pip 19.1.1 to easy-install.pth file
Installing pip script to /usr/local/bin
Installing pip3.7 script to /usr/local/bin
Installing pip3 script to /usr/local/bin

Using /usr/local/lib/python2.7/site-packages
Processing dependencies for pip
Finished processing dependencies for pip
Ryan-Cocuzzos-Laptop:~ Ryan$ brew install python
Updating Homebrew...
Warning: python 3.7.3 is already installed, it's just not linked
You can use `brew link python` to link this version.
Ryan-Cocuzzos-Laptop:~ Ryan$ brew link python
Linking /usr/local/Cellar/python/3.7.3... 
Error: Could not symlink bin/2to3
Target /usr/local/bin/2to3
already exists. You may want to remove it:
  rm '/usr/local/bin/2to3'

To force the link and overwrite all conflicting files:
  brew link --overwrite python

To list all files that would be deleted:
  brew link --overwrite --dry-run python
Ryan-Cocuzzos-Laptop:~ Ryan$ rm '/usr/local/bin/2to3'
Ryan-Cocuzzos-Laptop:~ Ryan$ brew link --overwrite python
Linking /usr/local/Cellar/python/3.7.3... 24 symlinks created
Ryan-Cocuzzos-Laptop:~ Ryan$ python -c "import ssl;print(ssl.OPENSSL_VERSION)"
OpenSSL 1.0.2r  26 Feb 2019
Ryan-Cocuzzos-Laptop:~ Ryan$ 
brew update && brew upgrade

brew uninstall --ignore-dependencies openssl; brew install https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb
brew reinstall python