ftp-client-1

使用語言:python 2.7
主要模塊:ftplib

主要使用一個while循環,沒有進行完整的異常處理,根據實際需求情況修改!

#coding=utf-8
from ftplib import FTP
import sys,os

target_host="192.168.142.134"
target_port="2121"
ftp_user="user"
ftp_pass="12345"
timeout=10
command=""
def ftp_cmd_list():
    print ("local      local directory")
    print ("cls        clear the screen")
    print ("pwd        print the name of current woring directory")
    print ("cd         change the working directory")
    print ("mkd        create new directory")
    print ("rmd        delete a directory")
    print ("dir        display the list of directory")
    print ("rename     rename a file")
    print ("del        delete a file")
    print ("quit       close connection politely")
    print ("close      clode connection impolitely")
    print ("upload     send files from local to remote server")
    print ("download   receive files from server")
def main():
    global target_host
    global target_port
    global ftp_user
    global ftp_pass
    global timeout
    global command
    parameter=len(sys.argv)
    if parameter!=1:
        print "don't add parameter"
    f=FTP()
    f.set_debuglevel(1)
    f.connect(host=target_host, port=target_port,timeout=timeout)
    f.login(user=ftp_user,passwd=ftp_pass)
    f.getwelcome()
    while True:
        command=raw_input(' <? to get help>## :')
        if command=='?'or command=='help':
            ftp_cmd_list()
        elif command=='pwd':
            f.pwd()  
        elif command=='dir':
            f.dir()
        elif command=='cd':
            dirname=raw_input('dirname:')
            f.cwd(dirname)
        elif command=='mkd':
            mkdir=raw_input('dirname:')
            f.mkd(mkdir)
        elif command=="rmd":
            rmdir=raw_input('dirname:')
            f.rmd(rmdir)
        elif command=='rename':
            fromname=raw_input('old name:')
            toname=raw_input('new name:')
            f.rename(fromname, toname)
        elif command=='del':
            delname=raw_input('filename:')
            f.delete(delname)
        elif command=='local':
            os.system("dir")
        elif command=='cls':
            os.system("cls")        
        elif command=='quit':
            f.quit()
            sys.exit(0)
        elif command=='close':
            f.close()
            sys.exit(0)
        elif command=='upload': 
            localfile=raw_input('localfile:')
            remotefile=raw_input('remotefile:')
            file_handler=open(localfile,'rb')
            f.storbinary('STOR %s'%remotefile,file_handler)
            file_handler.close()
        elif command=='download':
            remote_file=raw_input('remotefile:')
            local_file=raw_input('localfile:')
            file_hand=open(local_file,'wb')
            f.retrbinary('RETR %s'%(remote_file),file_hand.write)
        elif command=='stop':
            f.abort()
        else:
            print "this parameter is illegal"    

if __name__=='__main__':
    main()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章