mardi 1 décembre 2020

Python: Input accepts two+ arguments, but doesn't accept a single argument

My input accepts the input for two+ arguments, but doesn't accept when I need only a single argument; my code is suppose to be a "command line prompt" that is suppose to accept input from the user to create router interfaces and connections. I use argNum = len(delimitedInput), which is my input, and have the code follow based on the if statements. However, the if argNum == 1 is not working, while the other are working perfectly. Any help here is appreciated and here's my code below:

while 1:
    try:
        message, address = clientSocket.recvfrom(8192) # Buffer size is 8192. Change as needed.
        if message:
            print (address, "> ", message.decode())
    except:
        pass
 
    input = getLine();
    if(input != False):
        #output input
        print("input is: ", input)
        #split the input into delimitedInput 
        delimitedInput = input.split(' ') 
        #argNum is number of arguments the user typed 
        argNum = len(delimitedInput)
        command = delimitedInput[0] 
        print("The number of arguments is ", argNum) 
        #TODO: Accepting one argument 
        if argNum == 1:
            (command) = delimitedInput[0] 
            if command == "test":
                print("test")
            if command == "status":
                print("The router is listening and accepting connections to the following ports: ")
                print(mainlist)
                print("The router's name is ", routername)
            if command == "routerconnect":
                print("The following routers are connected to this router with the cost: ")
                print(connectionlist) 
        #Accepting two arguments
        if argNum == 2:
            port = delimitedInput[1] 
            (command, port) = delimitedInput
            #listenport + acceptport built into one command with a space to setup the listening port
            if command == "mainport":
                incomingPort = int(port)
                clientSocket.bind(('', incomingPort)) 
                mainlist.append(incomingPort)
                print("You are now listening and accepting connections to the this port:")
                print(mainlist)
                routername = "R" + str(incomingPort) 
                print("The router name is also now: ", routername)
        #Accepting three arguments
        if argNum == 3:
            port = delimitedInput[1]
            port2 = delimitedInput[2] 
            if command == "remote":
                #port = remote host address
                rmaddress.append(port)  
                #port2 = remote port 
                rmport.append(port2)
                remoteAddressAndPort = (port, int(port2))
                print("Command: ", command)
                print("Remote host address: ", port)
                print("Remote port: ", port2)
                clientSocket.sendto(input.encode(), remoteAddressAndPort)
        #Accepting four arguments 
        if argNum == 4:
            port = delimitedInput[1]
            port2 = delimitedInput[2]
            message = delimitedInput[3]  
            (command, port, port2, message) = delimitedInput 
            #Sending a message to other UDP chat hosts using the remoteport command
            if command == "send":
                #port = remote host address; this HAS TO BE 127.0.0.1 FOR SOME REASON 
                rmaddress.append(port)  
                #port2 = remote port 
                rmport.append(port2)
                remoteAddressAndPort = (port, int(port2))
                print("Command: ", command)
                print("Remote host address: ", port)
                print("Remote port: ", port2)
                print("Message: ", message) 
                clientSocket.sendto(input.encode(), remoteAddressAndPort)
            #setting up router cost
            if command == "cost":
                #port = remote host address, which has to be 127.0.0.1
                #port2 = remote port you want to connect to 
                #message = cost to move to that router
                message = int(message)
                port2 = ("R" + port2)   
                #Add to the connection/cost list
                connectdiction.append(str(port2) + " " + str(message))
                #Add to the "what is this connected to" list"
                connectionlist.append(port2)   
                print("Command: ", command)
                print("Remote host address, which has to be 127.0.0.1: ", port)
                print("Remote port, which should be the just the number: ", port2)
                print("Cost to connect to this router: ", message) 

Aucun commentaire:

Enregistrer un commentaire