From c118d899081bc62e28d47a5a0479fb16a24878ec Mon Sep 17 00:00:00 2001 From: Emiliano Ciavatta Date: Fri, 17 Apr 2020 15:01:20 +0200 Subject: Add import pcaps test --- scripts/generate_ping.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 scripts/generate_ping.py (limited to 'scripts') diff --git a/scripts/generate_ping.py b/scripts/generate_ping.py new file mode 100755 index 0000000..153a3cb --- /dev/null +++ b/scripts/generate_ping.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import struct +import socket +import socketserver +import sys +import time + +class PongHandler(socketserver.BaseRequestHandler): + + def handle(self): + def recv_data(): + return self.request.recv(1024).strip() + + data = recv_data() + while len(data) > 0: + counter = str(int(data) + 1).rjust(4, "0") + print(f"PONG! {counter}", end='\r') + self.request.sendall(counter.encode()) + data = recv_data() + + +if __name__ == "__main__": + if not ((sys.argv[1] == "server" and len(sys.argv) == 2) or + (sys.argv[1] == "client" and len(sys.argv) == 3)): + print(f"Usage: {sys.argv[0]} server") + print(f" {sys.argv[0]} client ") + exit(1) + + port = 9999 + n = 10000 + + if sys.argv[1] == "server": + # docker run -it --rm -p 9999:9999 -v "$PWD":/ping -w /ping python:3 python generate_ping.py server + with socketserver.TCPServer(("0.0.0.0", port), PongHandler) as server: + server.serve_forever() + else: + # docker run -it --rm -v "$PWD":/ping -w /ping python:3 python generate_ping.py client + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.connect((sys.argv[2], port)) + + counter = 0 + while counter < n: + counter = str(counter).rjust(4, "0") + print(f"PING! {counter}", end='\r') + time.sleep(0.05) + sock.sendall(counter.encode()) + counter = int(sock.recv(1024).strip()) + 1 + -- cgit v1.2.3-70-g09d2