|  | 
| 12 | 12 |  iperf3.client('192.168.1.5', udp=True, reverse=True) | 
| 13 | 13 | """ | 
| 14 | 14 | 
 | 
| 15 |  | -import sys, os, struct | 
|  | 15 | +import sys, struct | 
| 16 | 16 | import time, select, socket | 
| 17 | 17 | import json | 
| 18 | 18 | 
 | 
|  | 19 | +# Provide a urandom() function, supporting devices without os.urandom(). | 
|  | 20 | +try: | 
|  | 21 | + from os import urandom | 
|  | 22 | +except ImportError: | 
|  | 23 | + from random import randint | 
|  | 24 | + | 
|  | 25 | + def urandom(n): | 
|  | 26 | + return bytes(randint(0, 255) for _ in range(n)) | 
|  | 27 | + | 
|  | 28 | + | 
| 19 | 29 | DEBUG = False | 
| 20 | 30 | 
 | 
| 21 | 31 | # iperf3 cookie size, last byte is null byte | 
| @@ -177,7 +187,7 @@ def recvninto(s, buf): | 
| 177 | 187 | def make_cookie(): | 
| 178 | 188 |  cookie_chars = b"abcdefghijklmnopqrstuvwxyz234567" | 
| 179 | 189 |  cookie = bytearray(COOKIE_SIZE) | 
| 180 |  | - for i, x in enumerate(os.urandom(COOKIE_SIZE - 1)): | 
|  | 190 | + for i, x in enumerate(urandom(COOKIE_SIZE - 1)): | 
| 181 | 191 |  cookie[i] = cookie_chars[x & 31] | 
| 182 | 192 |  return cookie | 
| 183 | 193 | 
 | 
| @@ -243,7 +253,7 @@ def server_once(): | 
| 243 | 253 |  stats = Stats(param) | 
| 244 | 254 |  stats.start() | 
| 245 | 255 |  running = True | 
| 246 |  | - data_buf = bytearray(os.urandom(param["len"])) | 
|  | 256 | + data_buf = bytearray(urandom(param["len"])) | 
| 247 | 257 |  while running: | 
| 248 | 258 |  for pollable in poll.poll(stats.max_dt_ms()): | 
| 249 | 259 |  if pollable_is_sock(pollable, s_ctrl): | 
| @@ -445,7 +455,7 @@ def client(host, udp=False, reverse=False, bandwidth=10 * 1024 * 1024): | 
| 445 | 455 |  s_data = socket.socket(ai[0], socket.SOCK_STREAM) | 
| 446 | 456 |  s_data.connect(ai[-1]) | 
| 447 | 457 |  s_data.sendall(cookie) | 
| 448 |  | - buf = bytearray(os.urandom(param["len"])) | 
|  | 458 | + buf = bytearray(urandom(param["len"])) | 
| 449 | 459 |  elif cmd == EXCHANGE_RESULTS: | 
| 450 | 460 |  # Close data socket now that server knows we are finished, to prevent it flooding us | 
| 451 | 461 |  poll.unregister(s_data) | 
|  | 
0 commit comments