diff --git a/pyVim/connect.py b/pyVim/connect.py index 4ba2a19d9..936742678 100644 --- a/pyVim/connect.py +++ b/pyVim/connect.py @@ -23,25 +23,19 @@ Detailed description (for [e]pydoc goes here). """ - -import sys -import threading -import thread -import types -import httplib -import socket -import time -import itertools import re -from pyVmomi import vim, vmodl, SoapStubAdapter, SessionOrientedStub -from pyVmomi.VmomiSupport import nsMap, versionIdMap, versionMap, IsChildVersion -from pyVmomi.VmomiSupport import GetServiceVersions try: - from xml.etree.ElementTree import ElementTree + from xml.etree import ElementTree except ImportError: - from elementtree.ElementTree import ElementTree + from elementtree import ElementTree from xml.parsers.expat import ExpatError -import urllib2 + +import requests +from requests.auth import HTTPBasicAuth + +from pyVmomi import vim, vmodl, SoapStubAdapter, SessionOrientedStub +from pyVmomi.VmomiSupport import nsMap, versionIdMap, versionMap, IsChildVersion +from pyVmomi.VmomiSupport import GetServiceVersions """ @@ -57,7 +51,6 @@ @todo: Get rid of me? """ - class closing(object): """ Helper class for using closable objects in a 'with' statement, @@ -411,7 +404,7 @@ def __exit__(self, *exc_info): def __GetServiceVersionDescription(protocol, server, port, path): """ - Private method that returns an ElementTree describing the API versions + Private method that returns a root from an ElementTree describing the API versions supported by the specified server. The result will be vimServiceVersions.xml if it exists, otherwise vimService.wsdl if it exists, otherwise None. @@ -425,26 +418,23 @@ def __GetServiceVersionDescription(protocol, server, port, path): @type path: string """ - tree = ElementTree() - url = "%s://%s:%s/%s/vimServiceVersions.xml" % (protocol, server, port, path) try: - with closing(urllib2.urlopen(url)) as sock: - if sock.getcode() == 200: - tree.parse(sock) - return tree + sock = requests.get(url, verify=False) + if sock.status_code == 200: + tree = ElementTree.fromstring(sock.content) + return tree except ExpatError: pass url = "%s://%s:%s/%s/vimService.wsdl" % (protocol, server, port, path) try: - with closing(urllib2.urlopen(url)) as sock: - if sock.getcode() == 200: - tree.parse(sock) - return tree + sock = requests.get(url, verify=False) + if sock.status_code == 200: + tree = ElementTree.fromstring(sock.content) + return tree except ExpatError: pass - return None @@ -459,12 +449,12 @@ def __VersionIsSupported(desiredVersion, serviceVersionDescription): @param desiredVersion: The version we want to see if the server supports (eg. vim.version.version2. @type desiredVersion: string - @param serviceVersionDescription: An ElementTree for vimServiceVersions.xml + @param serviceVersionDescription: A root ElementTree for vimServiceVersions.xml or vimService.wsdl. - @type serviceVersionDescription: ElementTree + @type serviceVersionDescription: root ElementTree """ - root = serviceVersionDescription.getroot() + root = serviceVersionDescription if root.tag == 'namespaces': # serviceVersionDescription appears to be a vimServiceVersions.xml document if root.get('version') <> '1.0': @@ -593,11 +583,7 @@ def OpenUrlWithBasicAuth(url, user='root', pwd=''): the specified credentials to the server as part of the request. Returns the response as a file-like object. """ - pwMgr = urllib2.HTTPPasswordMgrWithDefaultRealm() - pwMgr.add_password(None, url, user, pwd) - handler = urllib2.HTTPBasicAuthHandler(pwMgr) - opener = urllib2.build_opener(handler) - return opener.open(url) + return requests.get(url, auth=HTTPBasicAuth(user, pwd), verify=False) def OpenPathWithStub(path, stub): """ @@ -617,8 +603,8 @@ def OpenPathWithStub(path, stub): raise vmodl.fault.NotSupported() hostPort = stub.host url = '%s://%s%s' % (protocol, hostPort, path) - request = urllib2.Request(url) + headers = {} if stub.cookie: - request.add_header('Cookie', stub.cookie) - return urllib2.urlopen(request) + headers["Cookie"] = stub.cookie + return requests.get(url, headers=headers, verify=False) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..663bd1f6a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file