当前位置: 首页 > 知识库问答 >
问题:

SSL请求失败-Python OSX

涂飞航
2023-03-14

使用Cookie访问经过身份验证的网页时,出现SSL错误。这似乎是一个mac特定的问题,许多人通过安装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。

共有2个答案

慕弘伟
2023-03-14

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,再次创建它,并安装所有依赖项。

董桐
2023-03-14

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

从https://stackoverflow.com/a/42098127/6018688

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

安装证书的位置。命令对于python安装可能与brew有所不同。

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

更新:权限拒绝错误清楚地指向缺乏安装权限。这个答案及其注释建议使用带有-H标志的sudo和sudo可能会帮助您解决这个问题:https://stackoverflow.com/a/49953581/6018688

 类似资料:
  • 问题内容: 每次我尝试做: 我收到此消息: 我尝试了一切: 更新我的要求 更新我的SSL 但没有任何变化。 我正在使用Python 2.7.6,无法更改此设置。 问题答案: 我最终解决了问题,将Ubuntu版本从14.04更新为14.10,问题已解决。 但是在旧版本的ubuntu和python中,我安装了这些lib,它似乎解决了所有问题 如果您没有安装pip2.7,则可以改用pip

  • 问题内容: 这段代码 给我这个错误 我对SSL几乎一无所知,但我曾尝试下载该站点的证书并使用该选项指向该文件,但是它没有用。我想念什么吗? 问题答案: 正如评论中已经指出的那样:从SSLLabs报告中可以看出,该网站的SSL实施不正确。该报告中有关您的问题的主要部分是: 该服务器的证书链不完整。等级上限为B。 这意味着服务器没有发送验证证书所需的完整证书链。这意味着您需要在验证时自行添加丢失的证书

  • 这是我的第一个问题。我对API上的http请求有问题。当我发出请求时,控制台上会显示以下错误: 异常:找不到不同类型'对象'的支持对象'[对象对象]'。NgFor仅支持绑定到Iterable,如Array。 每个文件的内容如下: 靴子ts 应用程序。组成部分ts peli.service.ts peli.ts 以及我从请求中收到的JSON文件(输入-蜘蛛侠): 我不知道问题出在哪里,一切似乎都很好

  • 类项目: hbm文件: 方法如下:

  • HTTP/1.1 408 REQUEST_TIMEOUT内容-长度:0连接:关闭 已关闭 --------------------------------------------------------------------------------------- 它会导致请求超时。如果我使用curl命令,它是成功的。 请求超时的原因是什么?