python3: urlib2 to requests
migrate from urllib2 to requests in the connect.py module. starting to fix issue #13 partial: https://github.com/vmware/pyvmomi/issues/55 see also: https://github.com/vmware/pyvmomi/pull/66
This commit is contained in:
committed by
Shawn Hartsock
parent
a526f4806e
commit
1fbab09276
@@ -33,15 +33,18 @@ import socket
|
|||||||
import time
|
import time
|
||||||
import itertools
|
import itertools
|
||||||
import re
|
import re
|
||||||
|
try:
|
||||||
|
from xml.etree import ElementTree
|
||||||
|
except ImportError:
|
||||||
|
from elementtree import ElementTree
|
||||||
|
from xml.parsers.expat import ExpatError
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from requests.auth import HTTPBasicAuth
|
||||||
|
|
||||||
from pyVmomi import vim, vmodl, SoapStubAdapter, SessionOrientedStub
|
from pyVmomi import vim, vmodl, SoapStubAdapter, SessionOrientedStub
|
||||||
from pyVmomi.VmomiSupport import nsMap, versionIdMap, versionMap, IsChildVersion
|
from pyVmomi.VmomiSupport import nsMap, versionIdMap, versionMap, IsChildVersion
|
||||||
from pyVmomi.VmomiSupport import GetServiceVersions
|
from pyVmomi.VmomiSupport import GetServiceVersions
|
||||||
try:
|
|
||||||
from xml.etree.ElementTree import ElementTree
|
|
||||||
except ImportError:
|
|
||||||
from elementtree.ElementTree import ElementTree
|
|
||||||
from xml.parsers.expat import ExpatError
|
|
||||||
import urllib2
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@@ -57,7 +60,6 @@ Global (thread-shared) ServiceInstance
|
|||||||
@todo: Get rid of me?
|
@todo: Get rid of me?
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class closing(object):
|
class closing(object):
|
||||||
"""
|
"""
|
||||||
Helper class for using closable objects in a 'with' statement,
|
Helper class for using closable objects in a 'with' statement,
|
||||||
@@ -417,7 +419,7 @@ class SmartConnection(object):
|
|||||||
|
|
||||||
def __GetServiceVersionDescription(protocol, server, port, path):
|
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
|
supported by the specified server. The result will be vimServiceVersions.xml
|
||||||
if it exists, otherwise vimService.wsdl if it exists, otherwise None.
|
if it exists, otherwise vimService.wsdl if it exists, otherwise None.
|
||||||
|
|
||||||
@@ -431,26 +433,23 @@ def __GetServiceVersionDescription(protocol, server, port, path):
|
|||||||
@type path: string
|
@type path: string
|
||||||
"""
|
"""
|
||||||
|
|
||||||
tree = ElementTree()
|
|
||||||
|
|
||||||
url = "%s://%s:%s/%s/vimServiceVersions.xml" % (protocol, server, port, path)
|
url = "%s://%s:%s/%s/vimServiceVersions.xml" % (protocol, server, port, path)
|
||||||
try:
|
try:
|
||||||
with closing(urllib2.urlopen(url)) as sock:
|
sock = requests.get(url, verify=False)
|
||||||
if sock.getcode() == 200:
|
if sock.status_code == 200:
|
||||||
tree.parse(sock)
|
tree = ElementTree.fromstring(sock.content)
|
||||||
return tree
|
return tree
|
||||||
except ExpatError:
|
except ExpatError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
url = "%s://%s:%s/%s/vimService.wsdl" % (protocol, server, port, path)
|
url = "%s://%s:%s/%s/vimService.wsdl" % (protocol, server, port, path)
|
||||||
try:
|
try:
|
||||||
with closing(urllib2.urlopen(url)) as sock:
|
sock = requests.get(url, verify=False)
|
||||||
if sock.getcode() == 200:
|
if sock.status_code == 200:
|
||||||
tree.parse(sock)
|
tree = ElementTree.fromstring(sock.content)
|
||||||
return tree
|
return tree
|
||||||
except ExpatError:
|
except ExpatError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@@ -465,12 +464,12 @@ def __VersionIsSupported(desiredVersion, serviceVersionDescription):
|
|||||||
@param desiredVersion: The version we want to see if the server supports
|
@param desiredVersion: The version we want to see if the server supports
|
||||||
(eg. vim.version.version2.
|
(eg. vim.version.version2.
|
||||||
@type desiredVersion: string
|
@type desiredVersion: string
|
||||||
@param serviceVersionDescription: An ElementTree for vimServiceVersions.xml
|
@param serviceVersionDescription: A root ElementTree for vimServiceVersions.xml
|
||||||
or vimService.wsdl.
|
or vimService.wsdl.
|
||||||
@type serviceVersionDescription: ElementTree
|
@type serviceVersionDescription: root ElementTree
|
||||||
"""
|
"""
|
||||||
|
|
||||||
root = serviceVersionDescription.getroot()
|
root = serviceVersionDescription
|
||||||
if root.tag == 'namespaces':
|
if root.tag == 'namespaces':
|
||||||
# serviceVersionDescription appears to be a vimServiceVersions.xml document
|
# serviceVersionDescription appears to be a vimServiceVersions.xml document
|
||||||
if root.get('version') <> '1.0':
|
if root.get('version') <> '1.0':
|
||||||
@@ -599,11 +598,7 @@ def OpenUrlWithBasicAuth(url, user='root', pwd=''):
|
|||||||
the specified credentials to the server as part of the request.
|
the specified credentials to the server as part of the request.
|
||||||
Returns the response as a file-like object.
|
Returns the response as a file-like object.
|
||||||
"""
|
"""
|
||||||
pwMgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
|
return requests.get(url, auth=HTTPBasicAuth(user, pwd), verify=False)
|
||||||
pwMgr.add_password(None, url, user, pwd)
|
|
||||||
handler = urllib2.HTTPBasicAuthHandler(pwMgr)
|
|
||||||
opener = urllib2.build_opener(handler)
|
|
||||||
return opener.open(url)
|
|
||||||
|
|
||||||
def OpenPathWithStub(path, stub):
|
def OpenPathWithStub(path, stub):
|
||||||
"""
|
"""
|
||||||
@@ -623,8 +618,8 @@ def OpenPathWithStub(path, stub):
|
|||||||
raise vmodl.fault.NotSupported()
|
raise vmodl.fault.NotSupported()
|
||||||
hostPort = stub.host
|
hostPort = stub.host
|
||||||
url = '%s://%s%s' % (protocol, hostPort, path)
|
url = '%s://%s%s' % (protocol, hostPort, path)
|
||||||
request = urllib2.Request(url)
|
headers = {}
|
||||||
if stub.cookie:
|
if stub.cookie:
|
||||||
request.add_header('Cookie', stub.cookie)
|
headers["Cookie"] = stub.cookie
|
||||||
return urllib2.urlopen(request)
|
return requests.get(url, headers=headers, verify=False)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user