在Python2.7.9的表单源安装之后,我得到ssl.SSLError:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:581)

时间:2015-01-14 11:06:52

标签: python python-2.7 ssl

我正在使用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

这里打破了什么?

1 个答案:

答案 0 :(得分:1)

我已经晚了几年,但我自己也遇到了这个问题,并找到了一个解决方案。如果将来有人偶然发现这个帖子,我就会发表评论。

原因为什么会发生这种情况(这是Patryk的问题),Python的httplib.HTTPSConnection以前没有默认验证SSL证书。从Python 2.7.9开始,SSL validation is required。如果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证书。