From 3e48c0b5d1b21554eea8d12bc66175a9157a6951 Mon Sep 17 00:00:00 2001 From: Anton Kiselev Date: Tue, 1 Nov 2016 19:19:48 +0700 Subject: scapy server python 3 compatibility fixes(file and pseudo-random generator) Signed-off-by: Anton Kiselev --- .../stl/services/scapy_server/scapy_service.py | 13 +++++++++---- .../stl/services/scapy_server/unit_tests/test_utils.py | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'scripts/automation/trex_control_plane/stl/services/scapy_server') diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py index 118d7d6e..80e2fc99 100755 --- a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py @@ -282,12 +282,17 @@ def get_sample_field_val(scapy_layer, fieldId): def generate_random_bytes(sz, seed, start, end): # generate bytes of specified range with a fixed seed and size - rnd = random.Random(seed) - end = end + 1 # to include end value - res = [rnd.randrange(start, end) for _i in range(sz)] + rnd = random.Random() + n = end - start + 1 if is_python(2): + rnd = random.Random(seed) + res = [start + int(rnd.random()*n) for _i in range(sz)] return ''.join(chr(x) for x in res) else: + rnd = random.Random() + # to generate same random sequence as 2.x + rnd.seed(seed, version=1) + res = [start + int(rnd.random()*n) for _i in range(sz)] return bytes(res) def generate_bytes_from_template(sz, template): @@ -363,7 +368,7 @@ class Scapy_service(Scapy_service_api): def _load_definitions_from_json(self): # load protocol definitions from a json file self.protocol_definitions = {} - with file('protocols.json') as f: + with open('protocols.json', 'r') as f: protocols = json.load(f) for protocol in protocols: self.protocol_definitions[ protocol['id'] ] = protocol diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_utils.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_utils.py index 82261859..c1fb0478 100644 --- a/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_utils.py +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_utils.py @@ -7,7 +7,7 @@ def test_generate_random_bytes(): res = generate_random_bytes(10, 333, ord('0'), ord('9')) print(res) assert(len(res) == 10) - assert(res == '5390532937') # random value with this seed + assert(res == b'5390532937') # random value with this seed def test_generate_bytes_from_template_empty(): res = generate_bytes_from_template(5, b"") -- cgit 1.2.3-korg