From ea21cb3fdcc8633bdaae11c41ad4255c5be7a793 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 21 Mar 2023 16:01:01 +1100 Subject: [PATCH] iperf3: Support devices without os.urandom(). Also add a version to the manifest. Signed-off-by: Damien George --- python-ecosys/iperf3/iperf3.py | 18 ++++++++++++++---- python-ecosys/iperf3/manifest.py | 2 ++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/python-ecosys/iperf3/iperf3.py b/python-ecosys/iperf3/iperf3.py index 59a4d690..614bc619 100644 --- a/python-ecosys/iperf3/iperf3.py +++ b/python-ecosys/iperf3/iperf3.py @@ -12,10 +12,20 @@ Usage: iperf3.client('192.168.1.5', udp=True, reverse=True) """ -import sys, os, struct +import sys, struct import time, select, socket import json +# Provide a urandom() function, supporting devices without os.urandom(). +try: + from os import urandom +except ImportError: + from random import randint + + def urandom(n): + return bytes(randint(0, 255) for _ in range(n)) + + DEBUG = False # iperf3 cookie size, last byte is null byte @@ -177,7 +187,7 @@ def recvninto(s, buf): def make_cookie(): cookie_chars = b"abcdefghijklmnopqrstuvwxyz234567" cookie = bytearray(COOKIE_SIZE) - for i, x in enumerate(os.urandom(COOKIE_SIZE - 1)): + for i, x in enumerate(urandom(COOKIE_SIZE - 1)): cookie[i] = cookie_chars[x & 31] return cookie @@ -243,7 +253,7 @@ def server_once(): stats = Stats(param) stats.start() running = True - data_buf = bytearray(os.urandom(param["len"])) + data_buf = bytearray(urandom(param["len"])) while running: for pollable in poll.poll(stats.max_dt_ms()): if pollable_is_sock(pollable, s_ctrl): @@ -445,7 +455,7 @@ def client(host, udp=False, reverse=False, bandwidth=10 * 1024 * 1024): s_data = socket.socket(ai[0], socket.SOCK_STREAM) s_data.connect(ai[-1]) s_data.sendall(cookie) - buf = bytearray(os.urandom(param["len"])) + buf = bytearray(urandom(param["len"])) elif cmd == EXCHANGE_RESULTS: # Close data socket now that server knows we are finished, to prevent it flooding us poll.unregister(s_data) diff --git a/python-ecosys/iperf3/manifest.py b/python-ecosys/iperf3/manifest.py index dafba2e1..23113692 100644 --- a/python-ecosys/iperf3/manifest.py +++ b/python-ecosys/iperf3/manifest.py @@ -1 +1,3 @@ +metadata(version="0.1.3") + module("iperf3.py")