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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值