Skip to content

Commit b55cfae

Browse files
wangxinvmittal-msft
authored andcommitted
Replace 'urllib2' with 'requests' for making HTTP requests (sonic-net#4105)
What is the motivation for this PR? The mux_simulator_control.py script uses 'urllib2' for making HTTP requests. This is incompatible between python2 and python3. How did you do it? This change replaced 'urllib2' with 'requests'. How did you verify/test it? Test run decap/test_decap.py on dualtor testbed with sanity check enabled. Signed-off-by: Xin Wang <[email protected]>
1 parent c97e346 commit b55cfae

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

tests/common/dualtor/mux_simulator_control.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import pytest
3-
import json
4-
import urllib2
3+
4+
import requests
55

66
from tests.common import utilities
77
from tests.common.helpers.assertions import pytest_assert
@@ -74,24 +74,22 @@ def _get(server_url):
7474
server_url: a str, the full address of mux server, like http://10.0.0.64:8080/mux/vms17-8[/1]
7575
Returns:
7676
dict: A dict decoded from server's response.
77-
None: Returns None is error is detected.
77+
None: Returns None if request failed.
7878
"""
79-
req = urllib2.Request(url=server_url)
8079
try:
81-
res = urllib2.urlopen(req)
82-
data = res.read()
83-
return json.loads(data)
84-
except urllib2.HTTPError as e:
85-
err_msg = json.loads(e.read().decode())['err_msg']
86-
logger.warn("get request returns err. status_code = {} err_msg = {}".format(e.code, err_msg))
87-
except urllib2.URLError as e:
88-
logger.warn("get request returns err. err_msg = {}".format(str(e)))
89-
except json.decoder.JSONDecodeError as e:
90-
logger.warn("failed to parse response as json. err_msg = {}".format(str(e)))
80+
logger.debug('GET {}'.format(server_url))
81+
headers = {'Accept': 'application/json'}
82+
resp = requests.get(server_url, headers=headers)
83+
if resp.status_code == 200:
84+
return resp.json()
85+
else:
86+
logger.warn("GET {} failed with {}".format(server_url, resp.text))
9187
except Exception as e:
92-
logger.warn("get request returns err. err_msg = {}".format(str(e)))
88+
logger.warn("GET {} failed with {}".format(server_url, repr(e)))
89+
9390
return None
9491

92+
9593
def _post(server_url, data):
9694
"""
9795
Helper function for posting data to y_cable server.
@@ -102,22 +100,15 @@ def _post(server_url, data):
102100
Returns:
103101
True if succeed. False otherwise
104102
"""
105-
data = json.dumps(data).encode(encoding='utf-8')
106-
header = {'Accept': 'application/json', 'Content-Type': 'application/json'}
107-
req = urllib2.Request(url=server_url, data=data, headers=header)
108103
try:
109-
_ = urllib2.urlopen(req)
110-
except urllib2.HTTPError as e:
111-
try:
112-
err_msg = json.loads(e.read().decode())['err_msg']
113-
logger.warn("post request returns err. status_code = {} err_msg = {}".format(e.code, err_msg))
114-
except Exception:
115-
logger.warn("post request returns err. status_code = {}".format(e.code))
116-
return False
117-
except urllib2.URLError as e:
118-
logger.warn("post request returns err. err_msg = {}".format(str(e)))
119-
return False
120-
return True
104+
logger.debug('POST {} with {}'.format(server_url, data))
105+
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
106+
resp = requests.post(server_url, json=data, headers=headers)
107+
return resp.status_code == 200
108+
except Exception as e:
109+
logger.warn("POST {} with data {} failed, err: {}".format(server_url, data, repr(e)))
110+
111+
return False
121112

122113

123114
@pytest.fixture(scope='function')
@@ -393,7 +384,7 @@ def reset_simulator_port(url):
393384

394385
def _reset_simulator_port(interface_name=None):
395386
logger.warn("Resetting simulator ports {}".format('all' if interface_name is None else interface_name))
396-
server_url = url(interface_name=interface_name, action=RESET)
387+
server_url = url(interface_name=interface_name, action=RESET)
397388
pytest_assert(_post(server_url, {}))
398389

399390
return _reset_simulator_port
@@ -410,4 +401,4 @@ def get_mux_status(url):
410401
def _get_mux_status(interface_name=None):
411402
return _get(url(interface_name=interface_name))
412403

413-
return _get_mux_status
404+
return _get_mux_status

0 commit comments

Comments
 (0)