:)

他很懒,什么都没有留下。

ftp-client-2

编程语言: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()
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ShadowFlow/article/details/51549529
个人分类: Python基础
上一篇ftp-client-1
下一篇ftp-server
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭