Skip to content

Commit 1c0f8f3

Browse files
Adding server mode and bind interface
Added support for server mode and binding to a specific network interface. Updated environment variable requirements and adjusted function parameters accordingly.
1 parent 6259c04 commit 1c0f8f3

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

speedtest.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
Configured via environment variables for containerized deployment.
55
66
Required environment variables:
7-
SERVER_IP - iperf3 server IP address
7+
MODE - 'client' or 'server'
8+
SERVER_IP - iperf3 server IP address (only required in client mode)
89
910
Optional environment variables (with defaults):
1011
PORT - Server port (default: 5201)
@@ -13,6 +14,7 @@
1314
OUTPUT_DIR - Output directory (default: /data)
1415
PROTOCOL - 'tcp' or 'udp' (default: tcp)
1516
BANDWIDTH - UDP bandwidth limit (default: 100M)
17+
BIND_INTERFACE - Network interface to bind to (e.g., eth0, wlan0)
1618
1719
Usage:
1820
podman run --env-file options.env -v ./results:/data:z image_name
@@ -26,7 +28,21 @@
2628
from pathlib import Path
2729

2830

29-
def run_tests(server_ip, port, duration, output_dir, test_count, protocol, bandwidth):
31+
def run_server(port, bind_interface=None):
32+
"""Run iperf3 in server mode."""
33+
print(f"Starting iperf3 server on port {port}")
34+
if bind_interface:
35+
print(f"Bound to interface: {bind_interface}")
36+
print("Waiting for connections...\n")
37+
38+
cmd = ['iperf3', '-s', '-p', str(port)]
39+
if bind_interface:
40+
cmd.extend(['-B', bind_interface])
41+
42+
subprocess.run(cmd)
43+
44+
45+
def run_tests(server_ip, port, duration, output_dir, test_count, protocol, bandwidth, bind_interface=None):
3046
"""Run download and upload iperf3 tests."""
3147
results = {}
3248

@@ -35,6 +51,8 @@ def run_tests(server_ip, port, duration, output_dir, test_count, protocol, bandw
3551
cmd = ['iperf3', '-c', server_ip, '-p', str(port), '-t', str(duration), '-R', '-J']
3652
if protocol == 'udp':
3753
cmd.extend(['-u', '-b', bandwidth])
54+
if bind_interface:
55+
cmd.extend(['-B', bind_interface])
3856

3957
result = subprocess.run(cmd, capture_output=True, text=True)
4058
data = json.loads(result.stdout)
@@ -58,6 +76,8 @@ def run_tests(server_ip, port, duration, output_dir, test_count, protocol, bandw
5876
cmd = ['iperf3', '-c', server_ip, '-p', str(port), '-t', str(duration), '-J']
5977
if protocol == 'udp':
6078
cmd.extend(['-u', '-b', bandwidth])
79+
if bind_interface:
80+
cmd.extend(['-B', bind_interface])
6181

6282
result = subprocess.run(cmd, capture_output=True, text=True)
6383
data = json.loads(result.stdout)
@@ -118,17 +138,28 @@ def save_to_csv(results, csv_file, test_number, protocol):
118138

119139
def main():
120140
# Read configuration from environment variables
141+
mode = os.environ.get('MODE', 'client').lower()
121142
server_ip = os.environ.get('SERVER_IP')
122143
port = int(os.environ.get('PORT', 5201))
123144
duration = int(os.environ.get('DURATION', 5))
124145
interval = int(os.environ.get('INTERVAL', 10))
125146
output_dir = Path(os.environ.get('OUTPUT_DIR', '/data'))
126147
protocol = os.environ.get('PROTOCOL', 'tcp').lower()
127148
bandwidth = os.environ.get('BANDWIDTH', '100M')
149+
bind_interface = os.environ.get('BIND_INTERFACE')
150+
151+
# Validate mode
152+
if mode not in ['client', 'server']:
153+
raise ValueError(f"MODE must be 'client' or 'server', got: {mode}")
154+
155+
# Server mode
156+
if mode == 'server':
157+
run_server(port, bind_interface)
158+
return
128159

129-
# Validate required parameters
160+
# Client mode - validate required parameters
130161
if not server_ip:
131-
raise ValueError("SERVER_IP environment variable is required")
162+
raise ValueError("SERVER_IP environment variable is required in client mode")
132163

133164
if protocol not in ['tcp', 'udp']:
134165
raise ValueError(f"PROTOCOL must be 'tcp' or 'udp', got: {protocol}")
@@ -141,13 +172,15 @@ def main():
141172
print(f"Testing {server_ip}:{port} using {protocol.upper()}")
142173
if protocol == 'udp':
143174
print(f"UDP bandwidth: {bandwidth}")
175+
if bind_interface:
176+
print(f"Bound to interface: {bind_interface}")
144177
print(f"Results: {csv_file}\n")
145178

146179
# Run continuous tests
147180
test_count = 0
148181
while True:
149182
test_count += 1
150-
results = run_tests(server_ip, port, duration, output_dir, test_count, protocol, bandwidth)
183+
results = run_tests(server_ip, port, duration, output_dir, test_count, protocol, bandwidth, bind_interface)
151184
save_to_csv(results, csv_file, test_count, protocol)
152185
time.sleep(interval)
153186

0 commit comments

Comments
 (0)