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

为君一笑 提交于 2019-12-14 02:34:33

问题


SmartConnect method throw error requests.exceptions.SSLError:[Errno8]_ssl.c ? how to resolve it

I am using pyvmomi-5.5.0.2014.1.1 package to automate the VMWare server. Using same credenticals through VMware ESXi 5.1, I am able to connect to server but when trying through code


    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
    

it throw error insecureplatformwarning and suggesting urllib3 url . After installing packages pyopenssl ndg-httpsclient pyasn1 as suggested in page then adding


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

in above code now I am getting error:

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

回答1:


For Python 3.4, only hevel answer worked for me. Here is a slightly shorter version:

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)

I was using Michaels solution sucessfully with pyvmomi on Python 2.x until I decided to migrate my pyvmomi library to Python 3 today. With Python 3.4 it had stopped working due to a slightly different ssl error:

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

From reverse engineering, I came to the same conclusion as hevel. In hindsight wishing I just came back to StackOverflow to double check the answers here.




回答2:


This worked for me 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

I just add this to my scripts.




回答3:


This worked for 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)



回答4:


Below code worked for me to connect to a host running ESXi6.5, using python version 3.7 and pyvmomi version 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()



回答5:


I use python3.6, give a full example below. It can work normally.

#!/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()

Reference of official github examples: https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/hello_world_vcenter.py



来源:https://stackoverflow.com/questions/31454255/pyvmomi-smartconnect-throw-error-requests-exceptions-sslerror-errno-8-ssl-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!