From 7777f0a7f46210bdb06d9f9138b42673eb0f1c2b Mon Sep 17 00:00:00 2001 From: chenyan Date: Thu, 30 Aug 2018 17:01:04 +0800 Subject: [PATCH] Fix urllib import issue for Python2/3 compatible code. Story: 2003429 Task: 24620 Change-Id: If45a091b0b5e3aa7597457a0c56dc46a3432312a Signed-off-by: chenyan Signed-off-by: Sun Austin --- cgcs-patch/cgcs-patch/cgcs_patch/patch_client.py | 7 ++++++- .../cgcs_patch_id/patch_id_allocator_client.py | 15 +++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/patch_client.py b/cgcs-patch/cgcs-patch/cgcs_patch/patch_client.py index 6f29e16e..ce44e15f 100644 --- a/cgcs-patch/cgcs-patch/cgcs_patch/patch_client.py +++ b/cgcs-patch/cgcs-patch/cgcs_patch/patch_client.py @@ -1215,7 +1215,12 @@ def check_for_os_region_name(): auth_token, endpoint = get_auth_token_and_endpoint(region) if endpoint is not None: global api_addr - from urlparse import urlparse + try: + # python 2 + from urlparse import urlparse + except ImportError: + # python 3 + from urllib.parse import urlparse url = urlparse(endpoint) address = format_url_address(url.hostname) api_addr = '{}:{}'.format(address, url.port) diff --git a/cgcs-patch/cgcs-patch/cgcs_patch_id/patch_id_allocator_client.py b/cgcs-patch/cgcs-patch/cgcs_patch_id/patch_id_allocator_client.py index 1da10caf..90cd11f9 100755 --- a/cgcs-patch/cgcs-patch/cgcs_patch_id/patch_id_allocator_client.py +++ b/cgcs-patch/cgcs-patch/cgcs_patch_id/patch_id_allocator_client.py @@ -5,11 +5,18 @@ # SPDX-License-Identifier: Apache-2.0 # -import urllib -import urllib2 import getopt import sys +try: + # Python 2 + from urllib import urlencode + from urllib2 import urlopen +except ImportError: + # Python 3 + from urllib.parse import urlencode + from urllib.request import urlopen + opts = ['sw_version=', 'prefix='] @@ -24,8 +31,8 @@ def request_patch_id(sw_version="1.01", prefix="CGCS"): print("raw_parms = %s" % str(raw_parms)) url = "http://%s:%d/get_patch_id" % (server, port) - params = urllib.urlencode(raw_parms) - response = urllib2.urlopen(url, params).read() + params = urlencode(raw_parms) + response = urlopen(url, params).read() return response