首页 > 解决方案 > SSL 请求失败 - Python OSX

问题描述

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

我应该如何解决这个错误?

代码:

...
import urllib.request

opener = urllib.request.build_opener()
print("Cookies: ", headers["Cookie"])
opener.addheaders.append(('Cookie', headers["Cookie"]))
f = opener.open("https://home.nest.com/home/"+url_addtl_data)
print(f)

错误信息:

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)>

尝试的解决方案:

Last login: Sat May 11 03:44:45 on ttys000
Ryan-Cocuzzos-Laptop:~ Ryan$ /Applications/Python\ 3.6/Install\ Certificates.command ; exit;
 -- pip install --upgrade certifi
Collecting certifi
  Using cached https://files.pythonhosted.org/packages/60/75/f692a584e85b7eaba0e03827b3d51f45f571c2e793dd731e598828d380aa/certifi-2019.3.9-py2.py3-none-any.whl
Installing collected packages: certifi
Exception:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/wheel.py", line 316, in clobber
    ensure_dir(destdir)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/os.py", line 220, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/certifi-2019.3.9.dist-info'
You are using pip version 9.0.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Traceback (most recent call last):
  File "<stdin>", line 44, in <module>
  File "<stdin>", line 25, in main
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6', '-E', '-s', '-m', 'pip', 'install', '--upgrade', 'certifi']' returned non-zero exit status 2.
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
Deleting expired sessions...30 completed.

[Process completed]

编辑 5/19

附加终端会话(来自建议的解决方案):

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$ 

编辑 5/21

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

在此处输入图像描述

标签: pythonpython-3.xsslpython-requestsssl-certificate

解决方案


不需要重新安装 Python 或 pip。

来自https://stackoverflow.com/a/42098127/6018688

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

Install Certificates.command对于 python 安装,的位置可能会有所不同brew

在这个环境中,还有其他关于证书和 openssl 的问题/答案,这里这里

更新:权限被拒绝错误清楚地表明缺少安装权限。此答案及其建议使用带有 -H 标志的 sudo 和 sudo 的评论可能会帮助您解决问题:https ://stackoverflow.com/a/49953581/6018688


推荐阅读