pyvmomi SmartConnect:抛出错误requests.exceptions.SSLError:[Errno 8] _ssl.c

时间:2015-07-16 12:31:40

标签: python pyvmomi

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
    

它抛出错误的不安全平台警告 并建议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>

5 个答案:

答案 0 :(得分:3)

这对我有用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

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

答案 1 :(得分:1)

这适用于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)

答案 2 :(得分:1)

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

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成功使用Michaels解决方案,直到我决定今天将pyvmomi库迁移到Python 3。使用Python 3.4,由于ssl错误略有不同,它已停止工作:

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

从逆向工程中,我得出了与hevel相同的结论。事后我希望我回到StackOverflow来仔细检查答案。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

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