From ec47819c43178e41834b517dd41d69525bf3d7da Mon Sep 17 00:00:00 2001 From: CarlosAndresB Date: Thu, 7 Dec 2017 12:12:29 +0100 Subject: [PATCH] Update IRIS-UDP.py --- IRIS-UDP.py | 77 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/IRIS-UDP.py b/IRIS-UDP.py index fb5c8ea..27a9e58 100644 --- a/IRIS-UDP.py +++ b/IRIS-UDP.py @@ -1,22 +1,69 @@ #!/usr/bin/python import socket +import getopt import sys import time -UDP_IP = "192.168.1.157" -UDP_PORT = 2390 -msg = "VAULT" - -try: - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.bind(("",8888)) -except: - sys.exit() +def usage(): + + print("Ayuda: IRIS UDP") + print("") + print("-d Direccion") + print("-p Puerto") + print("-m Mensaje") + print("-h Ayuda") -while(1): - #msgbts = bytes(msg, 'utf-8') - msgbts = str(msg).encode("utf-8") - s.sendto(msgbts, (UDP_IP, UDP_PORT)) - d = s.recvfrom(1024) #Prender Led cuando se recibe algo? - time.sleep(120) +def main(): + 'Main function' + global Action, x, y, z, UDP_IP, UDP_PORT, msgbts, s + + try: + opts, args = getopt.getopt(sys.argv[1:], "d:p:m:h") + + except getopt.GetoptError as err: + print("Error: "+str(err)+", -h para ayuda\n\n") + usage() + sys.exit(2) + + if len(opts) == 0: + print("Definir atgumentos, -h for ayuda") + sys.exit() + for o, a in opts: + if o in ("-h"): + usage() + sys.exit() + if o in ("-d"): + try: + UDP_IP = str(a).encode("utf-8") + except ValueError: + print("-- Direccion") + sys.exit(2) + if o in ("-p"): + try: + UDP_PORT=int(a) + except ValueError: + print ("--to argument is taking only numeric values") + sys.exit(2) + if o in ("-d"): + try: + msgbts = str(a).encode("utf-8") + except ValueError: + print("-- mensaje") + sys.exit(2) + + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.bind(("",8888)) + except: + sys.exit() + try: + d = s.recvfrom(1024) + print("Mensaje recibido:" + d) + except: + sys.exit() + + print("Listo") + +if __name__ == "__main__": + main()