FTP上传下载

Zss 发表于:

Tool.ftp_down(‘192.168.3.1′,’10021′,’down’,’down’,r’C:\Auto-Test\Data\Ftp\ftp-client-1\test.exe’)
Tool.ftp_up(‘192.168.3.1′,’10021′,’up’,’up’,r’C:\Auto-Test\Data\Ftp\ftp-client-2\test.exe’)

down  将down账号的test.exe 文件下载到C:\Auto-Test\Data\Ftp\ftp-client-1\test.exe

up 将C:\Auto-Test\Data\Ftp\ftp-client-1\test.exe上传到up账号根目录

from ftplib import FTP



class Tool():
    def __init__(self):
        pass

    @staticmethod
    #(ip,端口,账号,密码,下载到哪里去文件名是什么)
    def ftp_down(ip,port,username,password,donw_path):
        try:
            ftp_client = FTP()  # 实例化FTP对象
            ftp_client.connect(ip,int(port),10)
            ftp_client.login(username, password)
            print(ftp_client.getwelcome())
            f = open(donw_path,'wb')
            filename = 'RETR ' + donw_path.split('\\')[-1]
            ftp_client.retrbinary(filename,f.write,10240)
            f.close()
            ftp_client.quit()
        except Exception as e:
            print('ERROR:%s'%e)

    @staticmethod
    #(ip,端口,账号,密码,需要上传的文件路径)
    def ftp_up(ip,port,username,password,donw_path):
        try:
            ftp_client = FTP()  # 实例化FTP对象
            ftp_client.connect(ip,int(port),10)
            ftp_client.login(username, password)
            print(ftp_client.getwelcome())
            f = open(donw_path,'rb')
            filename = 'STOR ' + donw_path.split('\\')[-1]
            ftp_client.storbinary(filename,f,1024)
            ftp_client.quit()
        except Exception as e:
            print('ERROR:%s'%e)


if __name__ == '__main__':
    Tool.ftp_down('192.168.3.1','10021','down','down',r'C:\Auto-Test\Data\Ftp\ftp-client-1\test.exe')
    Tool.ftp_up('192.168.3.1','10021','up','up',r'C:\Auto-Test\Data\Ftp\ftp-client-2\test.exe')