1

I'm trying to implement a small script to manage a localhost with an FTP connection in Python from the command line and using the appropriate "ftplib" module. I would like to create a sort of raw input for the user but with some commands already setup.

I try to explain better:

Once I have created the FTP connection and login connection been successfully completed through username and password, i would show a sort of "bash shell" with the possibility to use the most famous UNIX commands ( for example cd and ls respectively to move in a directory and show file/folders in the current path ).

For example i could do this:

> cd "path inside localhost"

thus showing directories or:

> ls

to show all files and directories in that particular path. I've no idea how to implement this so i ask you some advices.

I thank you so much in advance for the help.

3
  • 1
    Have a look at curses module, and you can execute shell commands using subprocess module. Commented Apr 6, 2013 at 19:47
  • Or maybe os module? It has the listdir() and chdir() functions. Commented Apr 6, 2013 at 20:08
  • @AshwiniChaudhary I was searching for NotNamedDwayne solution, but thanks anyway for the precious advice. Commented Apr 6, 2013 at 20:12

1 Answer 1

3

It sounds like the command line interface is that part that you are asking about. One good way to map user inputs to commands is to use a dictionary and the fact that in python you can run a reference to a function by putting () after it's names. Here's a quick example showing you what I mean

def firstThing():  # this could be your 'cd' task
    print 'ran first task'

def secondThing(): # another task you would want to run
    print 'ran second task'

def showCommands(): # a task to show available commands
    print functionDict.keys()

# a dictionary mapping commands to functions (you could do the same with classes)
functionDict = {'f1': firstThing, 'f2': secondThing, 'help': showCommands}

# the actual function that gets the input
def main():
    cont = True
    while(cont):
        selection = raw_input('enter your selection ')
        if selection == 'q': # quick and dirty way to give the user a way out
            cont = False
        elif selection in functionDict.keys():
            functionDict[selection]()
        else:
            print 'my friend, you do not know me. enter help to see VALID commands'

if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.