From 56a74f912110994ed8fd4bdb78a2ebde9580a40d Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Sun, 12 Jun 2022 14:21:18 +0200 Subject: [PATCH] use args for python test script --- ESPFirewall/app.py | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/ESPFirewall/app.py b/ESPFirewall/app.py index 63d0cf1..021383d 100644 --- a/ESPFirewall/app.py +++ b/ESPFirewall/app.py @@ -1,11 +1,36 @@ -import socket - -sock = socket.socket() - -host = "10.93.0.246" -port = 80 - -sock.connect((host, port)) -sock.send("test".encode()) -sock.close() +import socket +import sys +import getopt +import argparse + +def main(argv): + parser = argparse.ArgumentParser( + description='Send socket message to ip and port.') + parser.add_argument('-i', '--ip', + default='localhost', + dest='ip', + help='Provide destination ip. Defaults to localhost', + type=str + ) + parser.add_argument('-p', '--port', + default=80, + dest='port', + help='Provide destination port. Defaults to 80', + type=int + ) + + args = parser.parse_args() + print(f'Sending message to {args.ip}:{args.port}') + sock = socket.socket() + try: + sock.connect((args.ip, args.port)) + sock.send("test".encode()) + print("Message sent.") + sock.close() + except: + print("Cannot send message...") + + +if __name__ == "__main__": + main(sys.argv[1:])