python之 ftp客户端的上传与下载

#-*- coding:utf-8 -*-

__author__ = 'call server'
__appname__ = "ftp_client"
__version__ = "1.0"

import ftplib
from socket import _GLOBAL_DEFAULT_TIMEOUT
import os

host = '127.0.0.1'
username = 'bl'
password = '123456'
class Ftp(ftplib.FTP):
    def __init__(self, host='127.0.0.1', user='bl', passwd='123456', acct='', timeout=_GLOBAL_DEFAULT_TIMEOUT):
        ftplib.FTP.__init__(self, host, user, passwd, acct, timeout)
        print self.getwelcome()  # 显示ftp服务器欢迎信息

        self.__ftp_dirs = None
        self.__ftp_files = None

    def __updatePath(self): #分辨是文件还是文件夹
        self.__ftp_dirs = []
        self.__ftp_files = []

        for file in self.nlst():
           try:
               self.cwd(file)
               self.cwd('..')
               self.__ftp_dirs.append(file.decode('utf-8'))
           except:
               self.__ftp_files.append(file.decode('utf-8'))

    def getPath(self):
        self.__updatePath()
        return self.__ftp_dirs, self.__ftp_files

    def downloadFiles(self, ftp_files, local_path, blocksize=8192):
        for ftp_file in ftp_files:
            self.__download(ftp_file, local_path, blocksize)

    def uploadFiles(self, local_files, ftp_path, blocksize=8192):
        for local_file in local_files:
            self.__upload(local_file, ftp_path, blocksize)

    def __download(self, ftp_file, local_path, blocksize=8192):
        local_file = os.path.join(local_path, os.path.basename(ftp_file))
        u8_ftp_file = ftp_file.encode('utf-8')
        try:
            print 'begin __downLoad ' + ftp_file
            fp = open(local_file, 'wb')
            self.retrbinary('RETR %s' %(u8_ftp_file), fp.write, blocksize)
            fp.close()
            print '__download ' + ftp_file + ' is end'
        except:
            print '__download ' + ftp_file + 'is fail'

    def __upload(self, local_file, ftp_path, blocksize=8192):
        try:
            print 'begin __upload ' + local_file
            u8_ftp_file = os.path.join(ftp_path, os.path.basename(local_file)).decode('utf-8')
            fp = open(local_file, 'rb')
            self.storbinary('STOR %s' %(u8_ftp_file), fp, blocksize)
            fp.close()
            print '__upload ' + local_file + ' is end'
        except:
            print '__upload ' + local_file + 'is fail'


if __name__ == '__main__':

    ftp = Ftp()

    dirs, files =  ftp.getPath()
    # for dir in dirs:
    #     print dir
    #
    # print '................'
    # for file in files:
    #     print file
    # ftp.downloadFiles(['mysql-installer-community-5.7.19.0.msi',
    #                    'Sublime Text Build 3176 x64 Setup.exe'], './')

    # ftp.uploadFiles(['mysql-installer-community-5.7.19.0.msi',
    #                  'Sublime Text Build 3176 x64 Setup.exe'], '/')
    pass
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值