Update urllib2 to be python3 compatable.
This change has no code impact on python2 There are multiple six.moves modules depending on which urllib2 component was being used. Classes were verified by print(class_name) Methods were verified using help(method_name) Error codes: urllib2.URLError -> from six.moves.urllib.error import URLError (verified as the same) urllib2.HTTPError from six.moves.urllib.error import HTTPError (verified as the same) Functionality: old: urllib2.HTTPHandler new: from six.moves.urllib.request import HTTPHandler verification: print(HTTPHandler) result: urllib2.HTTPHandler old: urllib2.Request new: from six.moves.urllib.request import Request verification: print(Request) result: urllib2.Request old: urllib2.build_opener new: from six.moves.urllib.request import build_opener verification: help(build_opener) result: Help on function build_opener in module urllib2: old: urllib2.install_opener new: from six.moves.urllib.request import install_opener verification: help(install_opener) result: Help on function install_opener in module urllib2: Story: 2004241 Task: 28069 Change-Id: Iec3a9e7b20c9d186313dfa4e152064283236603a Signed-off-by: Al Bailey <Al.Bailey@windriver.com>
This commit is contained in:
parent
1ed30fc137
commit
181d7b4832
@ -4,7 +4,9 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
#
|
#
|
||||||
import json
|
import json
|
||||||
import urllib2
|
|
||||||
|
from six.moves.urllib import error as urllib_error
|
||||||
|
from six.moves.urllib import request as urllib_request
|
||||||
|
|
||||||
from nfv_client.openstack.objects import Token
|
from nfv_client.openstack.objects import Token
|
||||||
|
|
||||||
@ -35,7 +37,7 @@ def get_token(auth_uri, project_name, project_domain_name, username, password,
|
|||||||
try:
|
try:
|
||||||
url = auth_uri + "/auth/tokens"
|
url = auth_uri + "/auth/tokens"
|
||||||
|
|
||||||
request_info = urllib2.Request(url)
|
request_info = urllib_request.Request(url)
|
||||||
request_info.add_header("Content-Type", "application/json")
|
request_info.add_header("Content-Type", "application/json")
|
||||||
request_info.add_header("Accept", "application/json")
|
request_info.add_header("Accept", "application/json")
|
||||||
|
|
||||||
@ -61,7 +63,7 @@ def get_token(auth_uri, project_name, project_domain_name, username, password,
|
|||||||
|
|
||||||
request_info.add_data(payload)
|
request_info.add_data(payload)
|
||||||
|
|
||||||
request = urllib2.urlopen(request_info)
|
request = urllib_request.urlopen(request_info)
|
||||||
# Identity API v3 returns token id in X-Subject-Token
|
# Identity API v3 returns token id in X-Subject-Token
|
||||||
# response header.
|
# response header.
|
||||||
token_id = request.info().getheader('X-Subject-Token')
|
token_id = request.info().getheader('X-Subject-Token')
|
||||||
@ -69,10 +71,10 @@ def get_token(auth_uri, project_name, project_domain_name, username, password,
|
|||||||
request.close()
|
request.close()
|
||||||
return Token(response, token_id)
|
return Token(response, token_id)
|
||||||
|
|
||||||
except urllib2.HTTPError as e:
|
except urllib_error.HTTPError as e:
|
||||||
print(e)
|
print(e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except urllib2.URLError as e:
|
except urllib_error.URLError as e:
|
||||||
print(e)
|
print(e)
|
||||||
return None
|
return None
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
#
|
#
|
||||||
import json
|
import json
|
||||||
import urllib2
|
from six.moves.urllib import error as urllib_error
|
||||||
|
from six.moves.urllib import request as urllib_request
|
||||||
from six.moves import http_client as httplib
|
from six.moves import http_client as httplib
|
||||||
|
|
||||||
|
|
||||||
@ -17,7 +18,7 @@ def request(token_id, method, api_cmd, api_cmd_headers=None, api_cmd_payload=Non
|
|||||||
'transfer-encoding', 'upgrade']
|
'transfer-encoding', 'upgrade']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
request_info = urllib2.Request(api_cmd)
|
request_info = urllib_request.Request(api_cmd)
|
||||||
request_info.get_method = lambda: method
|
request_info.get_method = lambda: method
|
||||||
if token_id is not None:
|
if token_id is not None:
|
||||||
request_info.add_header("X-Auth-Token", token_id)
|
request_info.add_header("X-Auth-Token", token_id)
|
||||||
@ -30,7 +31,7 @@ def request(token_id, method, api_cmd, api_cmd_headers=None, api_cmd_payload=Non
|
|||||||
if api_cmd_payload is not None:
|
if api_cmd_payload is not None:
|
||||||
request_info.add_data(api_cmd_payload)
|
request_info.add_data(api_cmd_payload)
|
||||||
|
|
||||||
url_request = urllib2.urlopen(request_info)
|
url_request = urllib_request.urlopen(request_info)
|
||||||
|
|
||||||
headers = list() # list of tuples
|
headers = list() # list of tuples
|
||||||
for key, value in url_request.info().items():
|
for key, value in url_request.info().items():
|
||||||
@ -49,7 +50,7 @@ def request(token_id, method, api_cmd, api_cmd_headers=None, api_cmd_payload=Non
|
|||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
except urllib2.HTTPError as e:
|
except urllib_error.HTTPError as e:
|
||||||
headers = list()
|
headers = list()
|
||||||
response_raw = dict()
|
response_raw = dict()
|
||||||
|
|
||||||
@ -96,7 +97,7 @@ def request(token_id, method, api_cmd, api_cmd_headers=None, api_cmd_payload=Non
|
|||||||
response_raw))
|
response_raw))
|
||||||
raise
|
raise
|
||||||
|
|
||||||
except urllib2.URLError as e:
|
except urllib_error.URLError as e:
|
||||||
print("Rest-API status=ERR, %s, %s, headers=%s, payload=%s"
|
print("Rest-API status=ERR, %s, %s, headers=%s, payload=%s"
|
||||||
% (method, api_cmd, api_cmd_headers, api_cmd_payload,))
|
% (method, api_cmd, api_cmd_headers, api_cmd_payload,))
|
||||||
raise
|
raise
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
import urllib2
|
|
||||||
|
|
||||||
|
from six.moves.urllib import request as urllib_request
|
||||||
from nfv_client import sw_update
|
from nfv_client import sw_update
|
||||||
|
|
||||||
|
|
||||||
@ -156,9 +156,9 @@ def process_main(argv=sys.argv[1:]): # pylint: disable=dangerous-default-value
|
|||||||
|
|
||||||
if args.debug:
|
if args.debug:
|
||||||
# Enable Debug
|
# Enable Debug
|
||||||
handler = urllib2.HTTPHandler(debuglevel=1)
|
handler = urllib_request.HTTPHandler(debuglevel=1)
|
||||||
opener = urllib2.build_opener(handler)
|
opener = urllib_request.build_opener(handler)
|
||||||
urllib2.install_opener(opener)
|
urllib_request.install_opener(opener)
|
||||||
|
|
||||||
if args.os_auth_url is None:
|
if args.os_auth_url is None:
|
||||||
args.os_auth_url = os.environ.get('OS_AUTH_URL', None)
|
args.os_auth_url = os.environ.get('OS_AUTH_URL', None)
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
#
|
#
|
||||||
import json
|
import json
|
||||||
import urllib2
|
from six.moves.urllib import error as urllib_error
|
||||||
|
from six.moves.urllib import request as urllib_request
|
||||||
|
|
||||||
from nfv_common import debug
|
from nfv_common import debug
|
||||||
|
|
||||||
@ -29,7 +30,7 @@ def get_token(directory):
|
|||||||
else:
|
else:
|
||||||
url = directory.auth_uri + "/v3/auth/tokens"
|
url = directory.auth_uri + "/v3/auth/tokens"
|
||||||
|
|
||||||
request_info = urllib2.Request(url)
|
request_info = urllib_request.Request(url)
|
||||||
request_info.add_header("Content-Type", "application/json")
|
request_info.add_header("Content-Type", "application/json")
|
||||||
request_info.add_header("Accept", "application/json")
|
request_info.add_header("Accept", "application/json")
|
||||||
|
|
||||||
@ -61,7 +62,7 @@ def get_token(directory):
|
|||||||
}}}})
|
}}}})
|
||||||
request_info.add_data(payload)
|
request_info.add_data(payload)
|
||||||
|
|
||||||
request = urllib2.urlopen(request_info)
|
request = urllib_request.urlopen(request_info)
|
||||||
# Identity API v3 returns token id in X-Subject-Token
|
# Identity API v3 returns token id in X-Subject-Token
|
||||||
# response header.
|
# response header.
|
||||||
token_id = request.info().getheader('X-Subject-Token')
|
token_id = request.info().getheader('X-Subject-Token')
|
||||||
@ -69,11 +70,11 @@ def get_token(directory):
|
|||||||
request.close()
|
request.close()
|
||||||
return Token(response, directory, token_id)
|
return Token(response, directory, token_id)
|
||||||
|
|
||||||
except urllib2.HTTPError as e:
|
except urllib_error.HTTPError as e:
|
||||||
DLOG.error("%s" % e)
|
DLOG.error("%s" % e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except urllib2.URLError as e:
|
except urllib_error.URLError as e:
|
||||||
DLOG.error("%s" % e)
|
DLOG.error("%s" % e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -8,7 +8,8 @@ import json
|
|||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
import urllib2
|
from six.moves.urllib import error as urllib_error
|
||||||
|
from six.moves.urllib import request as urllib_request
|
||||||
|
|
||||||
import BaseHTTPServer
|
import BaseHTTPServer
|
||||||
import SocketServer
|
import SocketServer
|
||||||
@ -298,7 +299,7 @@ def _rest_api_request(token_id, method, api_cmd, api_cmd_headers=None,
|
|||||||
start_ms = timers.get_monotonic_timestamp_in_ms()
|
start_ms = timers.get_monotonic_timestamp_in_ms()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
request_info = urllib2.Request(api_cmd)
|
request_info = urllib_request.Request(api_cmd)
|
||||||
request_info.get_method = lambda: method
|
request_info.get_method = lambda: method
|
||||||
request_info.add_header("X-Auth-Token", token_id)
|
request_info.add_header("X-Auth-Token", token_id)
|
||||||
request_info.add_header("Accept", "application/json")
|
request_info.add_header("Accept", "application/json")
|
||||||
@ -315,11 +316,11 @@ def _rest_api_request(token_id, method, api_cmd, api_cmd_headers=None,
|
|||||||
api_cmd_payload))
|
api_cmd_payload))
|
||||||
|
|
||||||
# Enable Debug
|
# Enable Debug
|
||||||
# handler = urllib2.HTTPHandler(debuglevel=1)
|
# handler = urllib_request.HTTPHandler(debuglevel=1)
|
||||||
# opener = urllib2.build_opener(handler)
|
# opener = urllib_request.build_opener(handler)
|
||||||
# urllib2.install_opener(opener)
|
# urllib_request.install_opener(opener)
|
||||||
|
|
||||||
request = urllib2.urlopen(request_info)
|
request = urllib_request.urlopen(request_info)
|
||||||
|
|
||||||
headers = list() # list of tuples
|
headers = list() # list of tuples
|
||||||
for key, value in request.info().items():
|
for key, value in request.info().items():
|
||||||
@ -351,7 +352,7 @@ def _rest_api_request(token_id, method, api_cmd, api_cmd_headers=None,
|
|||||||
response=response_raw,
|
response=response_raw,
|
||||||
execution_time=elapsed_secs))
|
execution_time=elapsed_secs))
|
||||||
|
|
||||||
except urllib2.HTTPError as e:
|
except urllib_error.HTTPError as e:
|
||||||
headers = list()
|
headers = list()
|
||||||
response_raw = dict()
|
response_raw = dict()
|
||||||
|
|
||||||
@ -412,7 +413,7 @@ def _rest_api_request(token_id, method, api_cmd, api_cmd_headers=None,
|
|||||||
api_cmd_payload, e.code, str(e),
|
api_cmd_payload, e.code, str(e),
|
||||||
str(e), headers, response_raw, reason)
|
str(e), headers, response_raw, reason)
|
||||||
|
|
||||||
except urllib2.URLError as e:
|
except urllib_error.URLError as e:
|
||||||
now_ms = timers.get_monotonic_timestamp_in_ms()
|
now_ms = timers.get_monotonic_timestamp_in_ms()
|
||||||
elapsed_ms = now_ms - start_ms
|
elapsed_ms = now_ms - start_ms
|
||||||
|
|
||||||
|
@ -455,10 +455,10 @@ def do_unit_tests(test_set=None, rest_api_debug=False, test_config=None):
|
|||||||
"""
|
"""
|
||||||
if rest_api_debug:
|
if rest_api_debug:
|
||||||
# Enable debugging of request and response headers for rest-api calls
|
# Enable debugging of request and response headers for rest-api calls
|
||||||
import urllib2
|
from six.moves.urllib import request as urllib_request
|
||||||
handler = urllib2.HTTPHandler(debuglevel=1)
|
handler = urllib_request.HTTPHandler(debuglevel=1)
|
||||||
opener = urllib2.build_opener(handler)
|
opener = urllib_request.build_opener(handler)
|
||||||
urllib2.install_opener(opener)
|
urllib_request.install_opener(opener)
|
||||||
|
|
||||||
platform_directory = openstack.get_directory(
|
platform_directory = openstack.get_directory(
|
||||||
config, openstack.SERVICE_CATEGORY.PLATFORM)
|
config, openstack.SERVICE_CATEGORY.PLATFORM)
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
#
|
#
|
||||||
import json
|
import json
|
||||||
import urllib2
|
from six.moves.urllib import error as urllib_error
|
||||||
|
from six.moves.urllib import request as urllib_request
|
||||||
|
|
||||||
from nfv_common import debug
|
from nfv_common import debug
|
||||||
|
|
||||||
@ -29,7 +30,7 @@ def validate_token(directory, admin_token, token_id):
|
|||||||
else:
|
else:
|
||||||
url = directory.auth_uri + "/v3/auth/tokens"
|
url = directory.auth_uri + "/v3/auth/tokens"
|
||||||
|
|
||||||
request_info = urllib2.Request(url)
|
request_info = urllib_request.Request(url)
|
||||||
request_info.add_header("Content-Type", "application/json")
|
request_info.add_header("Content-Type", "application/json")
|
||||||
request_info.add_header("Accept", "application/json")
|
request_info.add_header("Accept", "application/json")
|
||||||
request_info.add_header("X-Auth-Token", admin_token.get_id())
|
request_info.add_header("X-Auth-Token", admin_token.get_id())
|
||||||
@ -56,7 +57,7 @@ def validate_token(directory, admin_token, token_id):
|
|||||||
|
|
||||||
request_info.add_data(payload)
|
request_info.add_data(payload)
|
||||||
|
|
||||||
request = urllib2.urlopen(request_info)
|
request = urllib_request.urlopen(request_info)
|
||||||
# Identity API v3 returns token id in X-Subject-Token
|
# Identity API v3 returns token id in X-Subject-Token
|
||||||
# response header.
|
# response header.
|
||||||
token_id = request.info().getheader('X-Subject-Token')
|
token_id = request.info().getheader('X-Subject-Token')
|
||||||
@ -64,11 +65,11 @@ def validate_token(directory, admin_token, token_id):
|
|||||||
request.close()
|
request.close()
|
||||||
return Token(response, directory, token_id)
|
return Token(response, directory, token_id)
|
||||||
|
|
||||||
except urllib2.HTTPError as e:
|
except urllib_error.HTTPError as e:
|
||||||
DLOG.error("%s" % e)
|
DLOG.error("%s" % e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except urllib2.URLError as e:
|
except urllib_error.URLError as e:
|
||||||
DLOG.error("%s" % e)
|
DLOG.error("%s" % e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -85,7 +86,7 @@ def get_token(directory):
|
|||||||
else:
|
else:
|
||||||
url = directory.auth_uri + "/v3/auth/tokens"
|
url = directory.auth_uri + "/v3/auth/tokens"
|
||||||
|
|
||||||
request_info = urllib2.Request(url)
|
request_info = urllib_request.Request(url)
|
||||||
request_info.add_header("Content-Type", "application/json")
|
request_info.add_header("Content-Type", "application/json")
|
||||||
request_info.add_header("Accept", "application/json")
|
request_info.add_header("Accept", "application/json")
|
||||||
|
|
||||||
@ -117,7 +118,7 @@ def get_token(directory):
|
|||||||
}}}})
|
}}}})
|
||||||
request_info.add_data(payload)
|
request_info.add_data(payload)
|
||||||
|
|
||||||
request = urllib2.urlopen(request_info)
|
request = urllib_request.urlopen(request_info)
|
||||||
# Identity API v3 returns token id in X-Subject-Token
|
# Identity API v3 returns token id in X-Subject-Token
|
||||||
# response header.
|
# response header.
|
||||||
token_id = request.info().getheader('X-Subject-Token')
|
token_id = request.info().getheader('X-Subject-Token')
|
||||||
@ -125,11 +126,11 @@ def get_token(directory):
|
|||||||
request.close()
|
request.close()
|
||||||
return Token(response, directory, token_id)
|
return Token(response, directory, token_id)
|
||||||
|
|
||||||
except urllib2.HTTPError as e:
|
except urllib_error.HTTPError as e:
|
||||||
DLOG.error("%s" % e)
|
DLOG.error("%s" % e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except urllib2.URLError as e:
|
except urllib_error.URLError as e:
|
||||||
DLOG.error("%s" % e)
|
DLOG.error("%s" % e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
#
|
#
|
||||||
from six.moves import http_client as httplib
|
from six.moves import http_client as httplib
|
||||||
import urllib2
|
from six.moves.urllib import error as urllib_error
|
||||||
|
from six.moves.urllib import request as urllib_request
|
||||||
|
|
||||||
from nfv_common import debug
|
from nfv_common import debug
|
||||||
|
|
||||||
@ -20,7 +21,7 @@ def rest_api_request(token, method, url, headers=None, body=None):
|
|||||||
'transfer-encoding', 'upgrade']
|
'transfer-encoding', 'upgrade']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
request_info = urllib2.Request(url)
|
request_info = urllib_request.Request(url)
|
||||||
request_info.get_method = lambda: method
|
request_info.get_method = lambda: method
|
||||||
|
|
||||||
if headers is not None:
|
if headers is not None:
|
||||||
@ -38,11 +39,11 @@ def rest_api_request(token, method, url, headers=None, body=None):
|
|||||||
request_info.add_data(body)
|
request_info.add_data(body)
|
||||||
|
|
||||||
# Enable Debug
|
# Enable Debug
|
||||||
# handler=urllib2.HTTPHandler(debuglevel=1)
|
# handler = urllib_request.HTTPHandler(debuglevel=1)
|
||||||
# opener = urllib2.build_opener(handler)
|
# opener = urllib_request.build_opener(handler)
|
||||||
# urllib2.install_opener(opener)
|
# urllib_request.install_opener(opener)
|
||||||
|
|
||||||
request = urllib2.urlopen(request_info)
|
request = urllib_request.urlopen(request_info)
|
||||||
|
|
||||||
headers = list() # list of tuples
|
headers = list() # list of tuples
|
||||||
for key, value in request.info().items():
|
for key, value in request.info().items():
|
||||||
@ -54,7 +55,7 @@ def rest_api_request(token, method, url, headers=None, body=None):
|
|||||||
request.close()
|
request.close()
|
||||||
return httplib.OK, headers, response
|
return httplib.OK, headers, response
|
||||||
|
|
||||||
except urllib2.HTTPError as e:
|
except urllib_error.HTTPError as e:
|
||||||
if e.fp is not None:
|
if e.fp is not None:
|
||||||
headers = list() # list of tuples
|
headers = list() # list of tuples
|
||||||
for key, value in e.fp.info().items():
|
for key, value in e.fp.info().items():
|
||||||
|
Loading…
Reference in New Issue
Block a user