aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorEmiliano Ciavatta2020-04-17 13:01:20 +0000
committerEmiliano Ciavatta2020-04-17 13:01:20 +0000
commitc118d899081bc62e28d47a5a0479fb16a24878ec (patch)
tree6a24c1ac6d37cd0dc8957345db740e2dbfd7d0e7 /scripts
parent819d8af4f57e78724a2de02707bd4883715763bc (diff)
Add import pcaps test
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/generate_ping.py49
1 files changed, 49 insertions, 0 deletions
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 <server_address>")
+ 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 <server_address>
+ 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
+