After form-source installation of Python2.7.9 I get ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)

After form-source installation of Python2.7.9 I get ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)

我使用的是 CentOS 6.6。因此我没有我需要的 Python 的最新(2.7 系列)软件包。我已经从源代码安装了 Python 2.7.9 到 /usr/local/lib/python2.7 :

ll /usr/local/lib/python2.7/
total 52K
drwxr-xr-x. 2 root root 4.0K Jan 13 14:59 bin
drwxr-xr-x. 3 root root 4.0K Jan  7 15:15 include
drwxr-xr-x. 4 root root 4.0K Jan  7 15:15 lib
-rw-r--r--. 1 root root  12K Jan 14 11:46 LICENSE.txt
-rw-r--r--. 1 root root  117 Jan 14 11:46 MANIFEST.in
-rw-r--r--. 1 root root  238 Jan 14 11:46 NOTICE.txt
-rw-r--r--. 1 root root   88 Jan 14 11:46 setup.cfg
-rw-r--r--. 1 root root 2.6K Jan 14 11:46 setup.py
drwxr-xr-x. 3 root root 4.0K Jan  7 15:15 share
drwxr-xr-x. 6 root root 4.0K Jan  7 15:03 site-packages
-rw-r--r--. 1 root root  113 Jan 14 11:46 tox.ini

现在我有一个使用 pysphere 的脚本,它过去工作正常(在其他 2.6 和 2.7 机器上工作正常)但是当我 运行 它与我当前的设置我得到这个:

$ python main.py
Connecting to vSphere...
Traceback (most recent call last):
  File "main.py", line 51, in <module>
    server.connect(VSPHERE_IP, VSPHERE_USER, VSPHERE_PASS)
  File "/usr/local/lib/python2.7/lib/python2.7/site-packages/pysphere/vi_server.py", line 101, in connect
    request)._returnval
  File "/usr/local/lib/python2.7/lib/python2.7/site-packages/pysphere/resources/VimService_services.py", line 2170, in RetrieveServiceContent
    self.binding.Send(None, None, request, soapaction="urn:vim25/5.0", **kw)
  File "/usr/local/lib/python2.7/lib/python2.7/site-packages/pysphere/ZSI/client.py", line 295, in Send
    self.local.h.connect()
  File "/usr/local/lib/python2.7/lib/python2.7/httplib.py", line 1212, in connect
    server_hostname=server_hostname)
  File "/usr/local/lib/python2.7/lib/python2.7/ssl.py", line 350, in wrap_socket
    _context=self)
  File "/usr/local/lib/python2.7/lib/python2.7/ssl.py", line 566, in __init__
    self.do_handshake()
  File "/usr/local/lib/python2.7/lib/python2.7/ssl.py", line 788, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)

$ pip -V
pip 6.0.6 from /usr/local/lib/python2.7/lib/python2.7/site-packages (python 2.7)
$ pip freeze
Django==1.7.2
docutils==0.12
ecdsa==0.11
paramiko==1.15.2
pep8==1.5.7
pycrypto==2.6.1
Pygments==2.0.1
pysphere==0.1.7
pyvmomi==5.5.0.2014.1.1
requests==2.5.1
six==1.9.0
virtualenv==12.0.5

这里有什么问题?

我迟到了几年,但我自己 运行 解决了这个问题,并找到了 a 解决方案。如果将来有人偶然发现此线程,我将发表评论。

发生这种情况的原因(这是 Patryk 的问题)是 Python 的 httplib.HTTPSConnection previously did not validate SSL certificates by default. From Python 2.7.9 onwards, 。如果您的 ESX 服务器的 SSL 证书有问题,连接将失败。

不幸的是the pysphere library is no longer maintained

假设您正尝试在私有和安全的环境中使用 pysphere 库,您可以将以下补丁应用于 pysphere/ZSI/client.py 文件:

294c294,295
<         self.local.h = transport(netloc, None, **self.transdict)
---
>         import ssl
>         self.local.h = transport(netloc, None, context=ssl._create_unverified_context(), **self.transdict)

说明:pysphere库使用httplib.HTTPSConnection建立到ESX服务器的连接。如果它遇到 SSL 证书问题,它将失败并出现上述异常。要恢复 Python 2.7.9 之前的行为,您需要告诉 httplib.HTTPSConnection 不验证 SSL 证书。