关闭

ftp-client-2

标签: pythonpython-ftpftp客户端
101人阅读 评论(0) 收藏 举报
分类:

编程语言:python 2.7
主要模块:ftplib

使用cmd模块实现交互,没有完整的异常处理,根据实际情况修改!

#coding=utf-8
from cmd import Cmd  
from ftplib import FTP
import sys,os
target_host="192.168.142.134"
target_port="2121"
ftp_user="user"
ftp_pass="12345"
timeout=100 
def login_server():
    try:
        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()
        return f
    except:
        print "login failed !"
        sys.exit(0)    
class ftp_cmd(Cmd):
    def __init__(self):
        Cmd.__init__(self)
    def do_cls(self,line):
        os.system("cls")
    def do_local(self,line):
        os.system("dir")    
    def do_open(self,line):
        f=login_server()
        self.f=f
    def do_pwd(self,line):
        self.f.pwd()
    def do_cd(self,line):
        self.f.cwd(line)
    def do_mkdir(self,line):
        self.f.mkd(line)
    def do_rmdir(self,line):
        self.f.rmd(line)
    def do_dir(self,line):
        self.f.dir()
    def do_rename(self,line):
        filename=str(line).split(' ')
        self.f.rename(filename[0],filename[1])
    def do_del(self,line):
        self.f.delete(line)
    def do_quit(self,line):
        self.f.quit()
        sys.exit(0)
    def do_close(self,line):
        self.f.close()
        sys.exit(0)
    def do_upload(self,line):
        line=open(line,'rb')
        self.f.storbinary('STOR %s'%line,line)
        line.close()
    def do_download(self,line):
        filename=open(line,'wb')
        self.f.retrbinary('RETR %s'%line,filename.write)
        filename.close()
    def do_abort(self):
        self.f.abort()
def main():
    global target_host
    global target_port
    global ftp_user
    global ftp_pass
    global timeout
    parameter=len(sys.argv)
    if parameter!=1:
        print "-------------------------------------"
        print "Warnning : your parameters is illegal"
        print "-------------------------------------"
        sys.exit(0)
    else:
        ftp_cmd().cmdloop()
if __name__=='__main__':
    main()
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15264次
    • 积分:672
    • 等级:
    • 排名:千里之外
    • 原创:48篇
    • 转载:4篇
    • 译文:5篇
    • 评论:0条