天天看點

ftp中下載下傳檔案

使用Python編寫實作FTP檔案的下載下傳

代碼

from ftplib import FTP
import os
import sys
import time
import socket
import yaml
import configparser


class MyFTP:
    def __init__(self, host, port=21):
        """ 初始化 FTP 用戶端
        參數:
                 host:ip位址

                 port:端口号
        """
        self.host = host
        self.port = port
        self.ftp = FTP()
        # 重新設定下編碼方式
        self.ftp.encoding = 'utf-8'
        self.log_file = open("log.txt", "a")
        self.file_list = []

	""" 初始化 FTP 用戶端
	   參數:
	           username: 使用者名
	
	          password: 密碼
	 """
    def login(self, username, password):
        try:
            timeout = 60
            socket.setdefaulttimeout(timeout)
            # 0主動模式 1 #被動模式
            self.ftp.set_pasv(False)

            self.debug_print('開始嘗試連接配接到 %s' % self.host)
            self.ftp.connect(self.host, self.port)
            self.debug_print('成功連接配接到 %s' % self.host)

            self.debug_print('開始嘗試登入到 %s' % self.host)
            self.ftp.login(username, password)
            self.debug_print('成功登入到 %s' % self.host)

            self.debug_print(self.ftp.welcome)
        except Exception as err:
            self.deal_error("FTP 連接配接或登入失敗 ,錯誤描述為:%s" % err)
            pass
            
    """ 處理錯誤異常
        參數:
            e:異常
    """
    def deal_error(self, e):
        
        log_str = '發生錯誤: %s' % e
        self.write_log(log_str)
        sys.exit()

    def write_log(self, log_str):
        """ 記錄日志
            參數:
                log_str:日志
        """
        time_now = time.localtime()
        date_now = time.strftime('%Y-%m-%d', time_now)
        format_log_str = "%s ---> %s \n " % (date_now, log_str)
        print(format_log_str)
        self.log_file.write(format_log_str)
        
	 """從遠端目錄下載下傳多個檔案到本地目錄
	      參數:
	           local_path: 本地路徑
	
	           remote_path: 遠端路徑
	  """
    def download_file_tree(self, local_path, remote_path):
        try:
            print("download_file_tree()--->  local_path = %s ,remote_path = %s" % (local_path, remote_path))
            if not os.path.isdir(local_path):
                self.debug_print('本地目錄%s不存在,先建立本地目錄' % local_path)
                os.makedirs(local_path)

            try:
                os.chdir(local_path)
                self.ftp.cwd(remote_path)
            except Exception as err:
                self.debug_print('遠端目錄%s不存在,繼續...' % remote_path + " ,具體錯誤描述為:%s" % err)
                return

            self.debug_print('切換至目錄: %s' % self.ftp.pwd())

            self.file_list = []
            # 方法回調
            self.ftp.dir(self.get_file_list)
        except UnicodeDecodeError as err:
            print(err)
            pass
        finally:
            remote_names = self.file_list
            self.debug_print('遠端目錄 清單: %s' % remote_names)
            for item in remote_names:
                file_type = item[0]
                file_name = item[1]
                if ' ' in file_name:
                    file_name = file_name[file_name.index(' '):].strip()
                local = os.path.join(local_path, file_name)
                print('檔案名{0},是否是檔案:{1},檔案類型:{2}'.format(file_name, '.' in file_name, file_type))
                print(local)
                if '.' in file_name:  # 檔案
                    print(remote_path + ",download_file()---> 下載下傳檔案: %s" % file_name)
                    self.download_file(local, remote_path + '/' + file_name)
                else:  # 目錄
                    print(remote_path + ",download_file_tree()---> 下載下傳目錄: %s" % file_name + "/")
                    self.download_file_tree(local, remote_path + '/' + file_name)
                self.ftp.cwd("..")
                self.debug_print('傳回上層目錄 %s' % self.ftp.pwd())
            return True
            
 	"""從ftp下載下傳檔案
            參數:
                local_file: 本地檔案

                remote_file: 遠端檔案
    """
    def download_file(self, local_file, remote_file):
        self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))
        try:
            self.debug_print('>>>>>>>>>>>>下載下傳檔案 %s ... ...' % local_file)
            buf_size = 8024
            file_handler = open(local_file, 'wb')
            self.ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size)
            file_handler.close()
        except Exception as err:
            self.debug_print('下載下傳檔案出錯,出現異常:%s ' % err)
            return

	if __name__ == "__main__":
	    # 讀取配置檔案config.ini
	    cf = configparser.ConfigParser()
	    cf.read('config.ini')
	    cf.options('FTP-Config')
	    ftp_ip = cf.get('FTP-Config', 'ftp_ip')
	    user_name = cf.get('FTP-Config', 'user_name')
	    password = cf.get('FTP-Config', 'password')
	    client_path = cf.get('FTP-Config', 'client_path')
	    ftp_path = cf.get('FTP-Config', 'ftp_path')
	
	    # 初始化
	    my_ftp = MyFTP(str(ftp_ip))
	    # 連接配接并登入
	    my_ftp.login(str(user_name), str(password))
	    # 下載下傳目錄
	    my_ftp.download_file_tree(str(client_path), str(ftp_path))
	    my_ftp.close()
	    sys.exit()

           

配置檔案(config.ini)

[FTP-Config]
ftp_ip = 102.108.10.30
user_name = LDEYcs
password = LDEYcs
client_path = D:\landi
ftp_path = /DP/landi
           

遇到的問題:ftp下載下傳編碼問題(UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xcf in position 2: inva)

解決:剛開始用的是讀取yml檔案,後來改成讀取ini檔案解決
最初讀取檔案的方法:
	fileName = "config.yml"
    f = open(fileName, encoding='utf-8')
    address = f.read()
    yml = yaml.load(address, Loader=yaml.FullLoader)
最後使用的讀取方式:
	cf = configparser.ConfigParser()
    cf.read('config.ini')
    cf.options('FTP-Config')
    ftp_ip = cf.get('FTP-Config', 'ftp_ip')
    user_name = cf.get('FTP-Config', 'user_name')
    password = cf.get('FTP-Config', 'password')
    client_path = cf.get('FTP-Config', 'client_path')
    ftp_path = cf.get('FTP-Config', 'ftp_path')
           

打包

pyinstaller -F ***.py