Skip to content

Commit

Permalink
migrate from urllib2 to requests in the connect.py module. starting to
Browse files Browse the repository at this point in the history
fix issue vmware#13
  • Loading branch information
Michael Rice committed Jun 19, 2014
1 parent 16be269 commit 86ba173
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
64 changes: 25 additions & 39 deletions pyVim/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


"""
Expand All @@ -57,7 +51,6 @@
@todo: Get rid of me?
"""


class closing(object):
"""
Helper class for using closable objects in a 'with' statement,
Expand Down Expand Up @@ -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.
Expand All @@ -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


Expand All @@ -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':
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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)

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests

0 comments on commit 86ba173

Please sign in to comment.