pyvmomi SmartConnect: throw error requests.exceptions.SSLError: [Errno 8] _ssl.c

pyvmomi SmartConnect: throw error requests.exceptions.SSLError: [Errno 8] _ssl.c

SmartConnect 方法抛出错误 requests.exceptions.SSLError:[Errno8]_ssl.c?如何解决

我正在使用 pyvmomi-5.5.0.2014.1.1 包来自动化 VMWare 服务器。 通过 VMware ESXi 5.1 使用相同的凭据,我能够连接到服务器 但是在尝试通过代码时


    from pyVim import connect
    def connect_to_server(self, server=None, user=None, pwd=None, port=None):
    if server is None:
        server = _config_values("general", "host")
    if user is None:
        user = _config_values("general", "username")
    if pwd is None:
        pwd = _config_values("general", "password")
    if port is None:
        port = _config_values("general", "port")
    self._server = server
    self._user = user
    self._password = pwd
    self._port = int(port)
    try:
        service_instance = connect.SmartConnect("http", self._server,  self._port, self._user, self._password)
    except ConfigParseError:
        pass
    

它抛出错误 insecureplatformwarning 并建议 urllib3 url 。按照页面中的建议安装包 pyopenssl ndg-httpsclient pyasn1 之后 然后添加


    import urllib3.contrib.pyopenssl
    urllib3.contrib.pyopenssl.inject_into_urllib3()
    

在上面的代码中,现在出现错误:

<pre><code>    
Traceback (most recent call last):
File "D:\python_learning\vmwareATF\testVmwareatf.py", line 15, in <module>
main()
File "D:\python_learning\vmwareATF\testVmwareatf.py", line 12, in main
obj.connect_to_server()
File "D:\python_learning\vmwareATF\vmwareatf\vmware.py", line 52,in      connect_to_server
service_instance = connect.SmartConnect( host=self._server,    port=self._port,  user=self._user, pwd=self._password)
File "C:\Python27\lib\site-packages\pyVim\connect.py", line 577, in SmartConnect  preferredApiVersions)
File "C:\Python27\lib\site-packages\pyVim\connect.py", line 520, in  __FindSupportedVersion    path)
File "C:\Python27\lib\site-packages\pyVim\connect.py", line 435, in __GetServiceVersionDescription
sock = requests.get(url, verify=False)
File "C:\Python27\lib\site-packages\requests\api.py", line 69, in get
return request('get', url, params=params, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 465, in  request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 431, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [Errno bad handshake] (-1, 'Unexpected EOF')
</code></pre>

这对我有用http://www.errr-online.com/index.php/2015/05/09/how-to-fix-ssl-issues-with-pyvmomi-and-python-2-7-9/

import requests
requests.packages.urllib3.disable_warnings()

import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    # Legacy Python that doesn't verify HTTPS certificates by default
    pass
else:
     # Handle target environment that doesn't support HTTPS verification
    ssl._create_default_https_context = _create_unverified_https_context

我只是将其添加到我的脚本中。

这适用于 python2.7.9

sslContext = ssl.create_default_context()
sslContext.check_hostname = False
sslContext.verify_mode = ssl.CERT_NONE
si = SmartConnect(host=host,user=username,pwd=password,sslContext=sslContext)

对于 Python 3.4,只有 答案对我有用。这是一个略短的版本:

sslContext = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
sslContext.verify_mode = ssl.CERT_NONE
si = SmartConnect(host=host,user=username,pwd=password,sslContext=sslContext)

我在 Python 2.x 上通过 pyvmomi 成功使用 解决方案,直到今天我决定将我的 pyvmomi 库迁移到 Python 3。对于 Python 3.4,由于 ssl 错误略有不同,它已停止工作:

ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed...

从逆向工程,我得出了和hevel一样的结论。事后看来,希望我刚刚回到 Whosebug 仔细检查这里的答案。

我用的是python3.6,下面举个完整的例子。可以正常工作。

#!/usr/bin/env python3.6
# encoding: utf-8

from pyVim import connect
import ssl


def login():
    ssl_context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
    ssl_context.verify_mode = ssl.CERT_NONE
    si = connect.SmartConnect(host='192.168.0.1', user='root', pwd='password',
                              sslContext=ssl_context)
    print(si)
    print('If you got here, you authenticted into vCenter.')


if __name__ == '__main__':
    login()

参考官方github例子:https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/hello_world_vcenter.py

下面的代码让我连接到主机 运行 ESXi6.5,使用 python 版本 3.7 和 pyvmomi 版本 6.7.0.2018.9

from pyVim import connect
import ssl
def login():
    si = connect.ConnectNoSSL(host='192.168.1.123', user='root', pwd='password')
    print(si.content)

if __name__ == '__main__':
    login()