From a125fb94272657c17f7c978b265811dadc9a6add Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 25 Jul 2022 12:37:45 +0200 Subject: [PATCH] Add tcp to the tester --- ESPFirewall/app.py | 35 ------------------------------ tester.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 35 deletions(-) delete mode 100644 ESPFirewall/app.py create mode 100644 tester.py diff --git a/ESPFirewall/app.py b/ESPFirewall/app.py deleted file mode 100644 index 0519020..0000000 --- a/ESPFirewall/app.py +++ /dev/null @@ -1,35 +0,0 @@ -import socket -import sys -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:]) diff --git a/tester.py b/tester.py new file mode 100644 index 0000000..096e736 --- /dev/null +++ b/tester.py @@ -0,0 +1,54 @@ +import socket +import sys +import argparse + +message = "test" + + +def main(argv): + parser = argparse.ArgumentParser( + description='Send socket message to ip and port with protocol.') + 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 + ) + parser.add_argument('-t', '--type', choices=['UDP', 'TCP'], + default='UDP', + dest='proto', + help='Provide protocol. Defaults to UDP', + type=str + ) + args = parser.parse_args() + + print(f'Sending message to {args.ip}:{args.port} width {args.proto}') + + if args.proto == 'TCP': + sock = socket.socket() + try: + sock.connect((args.ip, args.port)) + sock.send(message.encode()) + print("Message sent.") + sock.close() + except: + print("Cannot send message...") + + else: + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + sock.sendto(message.encode('utf-8'), (args.ip, args.port)) + print("Message sent.") + sock.close() + except: + print("Cannot send message...") + + +if __name__ == "__main__": + main(sys.argv[1:])