pdc_project/test_server.py
2025-05-25 16:35:41 +02:00

56 lines
No EOL
1.7 KiB
Python

# test_server.py
#!/usr/bin/env python3
import argparse
import subprocess
import numpy as np
from encoder import make_codebook, encode_message
from decoder import decode_blocks, count_errors
def call_client(input_path, output_path, host, port):
subprocess.run([
"python3", "client.py",
f"--input_file={input_path}",
f"--output_file={output_path}",
f"--srv_hostname={host}",
f"--srv_port={port}"
], check=True)
def main():
parser = argparse.ArgumentParser(description="Server test using client.py")
parser.add_argument("--message", required=True, help="40-character message to send")
parser.add_argument("--srv_hostname", default="iscsrv72.epfl.ch", help="Server hostname")
parser.add_argument("--srv_port", type=int, default=80, help="Server port")
args = parser.parse_args()
msg = args.message
if len(msg) != 40:
raise ValueError("Message must be exactly 40 characters.")
C = make_codebook(r=5, num_blocks=len(msg))
x = encode_message(msg, C)
# write encoded symbols to fixed input.txt
input_file = "input.txt"
output_file = "output.txt"
np.savetxt(input_file, x)
# run client.py to read input.txt and write output.txt
call_client(input_file, output_file, args.srv_hostname, args.srv_port)
# read received samples
Y = np.loadtxt(output_file)
msg_hat = decode_blocks(Y, C)
print(f"↓ Decoded message: {msg_hat}")
errors = count_errors(msg, msg_hat)
print(f"Errors: {len(errors)} / {len(msg)} characters differ")
if errors:
for i, o, e in errors:
print(f" Pos {i}: sent '{o}' but got '{e}'")
else:
print("✔️ No decoding errors!")
if __name__ == "__main__":
main()