From c13680eeec20e1f41ad3b2727b4101eab0c20259 Mon Sep 17 00:00:00 2001 From: Alessandro Pilotti Date: Mon, 8 Sep 2014 15:29:49 +0300 Subject: [PATCH] Adds Python 3.x support in utils network module Replaces urllib2 usage with the six equivalent. --- cloudbaseinit/tests/utils/test_network.py | 2 +- cloudbaseinit/utils/network.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cloudbaseinit/tests/utils/test_network.py b/cloudbaseinit/tests/utils/test_network.py index e2541ed8..df3ed67d 100644 --- a/cloudbaseinit/tests/utils/test_network.py +++ b/cloudbaseinit/tests/utils/test_network.py @@ -27,7 +27,7 @@ CONF = cfg.CONF class NetworkUtilsTest(unittest.TestCase): @mock.patch('cloudbaseinit.osutils.factory.get_os_utils') - @mock.patch('urlparse.urlparse') + @mock.patch('six.moves.urllib.parse.urlparse') def _test_check_metadata_ip_route(self, mock_urlparse, mock_get_os_utils, side_effect): mock_utils = mock.MagicMock() diff --git a/cloudbaseinit/utils/network.py b/cloudbaseinit/utils/network.py index f5cd5c19..d95f4d9a 100644 --- a/cloudbaseinit/utils/network.py +++ b/cloudbaseinit/utils/network.py @@ -13,8 +13,9 @@ # under the License. import sys -import urllib2 -import urlparse + +from six.moves.urllib import parse +from six.moves.urllib import request from cloudbaseinit.openstack.common import log as logging from cloudbaseinit.osutils import factory as osutils_factory @@ -28,7 +29,7 @@ def check_url(url, retries_count=MAX_URL_CHECK_RETRIES): for i in range(0, MAX_URL_CHECK_RETRIES): try: LOG.debug("Testing url: %s" % url) - urllib2.urlopen(url) + request.urlopen(url) return True except Exception: pass @@ -44,7 +45,7 @@ def check_metadata_ip_route(metadata_url): if sys.platform == 'win32' and osutils.check_os_version(6, 0): # 169.254.x.x addresses are not getting routed starting from # Windows Vista / 2008 - metadata_netloc = urlparse.urlparse(metadata_url).netloc + metadata_netloc = parse.urlparse(metadata_url).netloc metadata_host = metadata_netloc.split(':')[0] if metadata_host.startswith("169.254."):