#!/usr/bin/env python # # This is a very simple, rudimentary and stupid FTP client written in python. # # Copyright (C) 2004-2007 Lars Engels # Use and distribute under BSD license. # import ftplib import getpass import os import sys from string import * # write a block to local file, print . as progress indicator def handleDownload(block): local.write(block) sys.stdout.write('.') # print content of current directory def ls(): print "Listing directory:\n" ftp.retrlines('LIST') # change directory def cd(): dir = raw_input("Directory: ") print "Changing directory to " + dir try: ftp.cwd(dir) print "\nContents of " + dir + "\n" ftp.retrlines('LIST') except ftplib.error_perm: print "Cannot open " + dir def cld(): localdir = raw_input("Local directory: ") try: os.chdir(localdir) print "\nChanged local directory to " + os.getcwd() except: print "Cannot change local directory to " + localdir host = raw_input("Hostname: ") host = host.strip() while host == "": host = raw_input("Hostname: ") host = host.strip() # generate ftp handle try: ftp = ftplib.FTP(host) except: print "Cannot connect to " + host sys.exit() anon = raw_input("Anonymous? [y/n]: ") while anon != "" and anon !="n" and anon !="y": print "Please enter 'y' or 'n'" anon = raw_input("Anonymous? [y/n]: ") if anon == "n": user = raw_input("Username: ") passwd = getpass.getpass() elif anon == "y" or anon == "": user = "anonymous" passwd = "script@python.org" print "Logging in..." try: print ftp.login(user, passwd) # print server's welcome message print ftp.getwelcome() print "\n" print "Listing directory:" ftp.retrlines('LIST') except ftplib.error_perm: print "Cannot log into " + host ftp.close() sys.exit() menu = 1 # user menu while menu != "8": print "\n.======================================." print "| You have the following options: |" print "|======================================|" print "| [ 1 | cd ] - Change directory |" print "| [ 2 | get ] - Download file |" print "| [ 3 | put ] - Upload file |" print "| [ 4 | ls ] - List directory |" print "| [ 5 | md ] - Create directory |" print "| [ 6 | rmd ] - Delete directory |" print "| [ 7 | cld ] - Change local directory |" print "| [ 8 | bye ] - Disconnect & Exit |" print "`======================================'" menu = raw_input("\nYour Choice: ") menu = menu.strip() # Change Directory if menu == "1" or menu == "cd": cd() # Get / Download elif menu == "2" or menu == "get": try: filename = raw_input("Filename to download: ") filename = filename.strip() try: local = open(filename, 'wb') except IOError: print "Cannot write to local directory. Missing access rights?" ftp.retrbinary('RETR ' + filename, handleDownload) print "\n" + filename + " downloaded successfully" local.close() except(ftplib.error_perm): print "Cannot download file " + filename # Put / Upload elif menu == "3" or menu == "put": try: filename = raw_input("Filename to upload: ") filename = filename.strip() if filename == "*": try: local = open(filename, 'rb') print "Uploading " + filename except IOError: print "Cannot open local file " + filename ftp.storbinary('STOR ' + filename, local) local.close() except ftplib.error_perm: print "Cannot upload file " + filename # ls / List elif menu == "4" or menu == "ls": ls() # md / Create directory elif menu == "5" or menu == "md": directory = raw_input("Directory to be created: ") try: ftp.mkd(directory) print "Successfully created directory " + directory except ftplib.error_perm: print "Cannot create directory " + directory # rmd / Delete directory elif menu == "6" or menu == "rmd": directory = raw_input("Directory to be deleted: ") try: print ftp.rmd(directory) except ftplib.error_perm: print "Cannot delete directory " + directory # cld / Change local directory elif menu == "7" or menu == "cld": cld() # bye / Quit elif menu == "8" or menu == "bye": print "Closing FTP connection..." ftp.close() print "FTP connection closed" sys.exit() else: print "Wrong selection, please check your input."