简体   繁体   中英

Issues with input() and raw_input() for Python 2.7. User entered data is not read properly. What is going on?

So at my company they are making me use python 2.7 because of the product for a compatibility reason that I won't get into here.

So I am writing a program that connects to a device using SSH (a switch specifically) and I am able to actually access the device using SSH and this device is ping-able on my machine. The problem ? raw_input seems to not be taking it as a string. When I try input(), it gives me an invalid syntax error.

For the scripts I write, I usually use arparse and the user enters the IP address, username, and password through the terminal, but I want this script to not use argparse and to use input() or raw_input. All my SSH scripts work good except for this one, the only one using raw_input and input() instead of argparse

def runMain():

    scriptName = os.path.basename(__file__)

    print("The name of this script is: " + scriptName)

    print("**************************************\n")

    print("This script allows you to enable and disable ports on the SNET or SOOBM switches, have fun ! \n")

    print("**************************************\n")

    optionPrinter_switches_top()

    user_input = raw_input("Make your selection") # This does not work if I change to input(), it exits out of the program

    if user_input == 1:
        print("You selected the SNET switch, lets proceed !")

        deviceIP = input("Enter the IP address for this device")  # I tried changing these to raw_input, I get a syntax issue
        deviceUsername = input("Enter the username for this device")
        devicePassword = input("Enter the password for this device")

        confirm_ping = canPing(deviceIP) # This is a boolean function that works correctly in every script but this one.

        if confirm_ping:
            ssh_SNET = connectToSSH_SNET(deviceIP, deviceUsername, devicePassword)
        else:
           print("Sorry, that device is not even ping-able. Figure that issue out and retry the program...")
           sys.exit(-1)

        while True:
            SNET_options()

            user_choice_SNET = input("Please select an option")

            switch_result = SNET_switch_func(user_choice_SNET)

            if switch_result == "displayInterfaceBrief":
                time.sleep(5)
                displayInterfaceBrief_SNET(ssh_SNET)
            elif switch_result == "enablePort":
                time.sleep(5)
                enablePort_SNET(ssh_SNET)
            elif switch_result == "disablePort":
                disablePort_SNET(ssh_SNET)
            elif switch_result == "switchReboot":
                reboot_SNET_switch(ssh_SNET)
            else:
                print("Exiting program now....")
                sys.exit(-1)

Here are relevant issues:

user_input = raw_input("Make your selection") # This does not work if I change to input(), it exits out of the program
 deviceIP = input("Enter the IP address for this device")  # I tried changing these to raw_input, I get a syntax issue
 deviceUsername = input("Enter the username for this device")
 devicePassword = input("Enter the password for this device")

confirm_ping = canPing(deviceIP) # This is a boolean function that works correctly in every script but this one.

Conclusion ? There is an issue with input()/raw_input() . What is going on here and how can I fix this ? I can't use python 3.7 and it really is frustrating. Thanks for the help

Try changing if user_input == 1: to if int(user_input) == 1: as the input function takes an input in string format, by default.

And, if you want to use input() instead of raw_input() to take input from users in python 2.x, then you can try below code:

if hasattr(__builtins__, 'raw_input'):
    input=raw_input

user_input = input("Enter a number: ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM