python实现FTP上传下载文件

python实现FTP上传下载文件

直接扔代码了

# -*- coding: utf-8 -*-
# Time     :2021/8/11 14:48
# Author   :syx
from ftplib import FTP
import os
import datetime
import sys
import re

class FtpGet(object):
    def __init__(self):
        self.config = Config()
        self.yesterday = self.__get_before_date()
    def __get_before_date (self):
        dt = str(datetime.date.today() - datetime.timedelta(days=1))
        yesterday = dt.replace("-", "")
        return yesterday
    def ftp_connect(self,proble_ip):
        ftp = FTP()
        ftp.set_debuglevel(1)
        ftp.connect(host=proble_ip, port=self.config.FTP_PORT)
        ftp.login(self.config.FTP_USER,self.config.FTP_PASSWORD)
        return ftp
    def create_dir (self,dir_name):
        if os.path.isdir(dir_name):
            print("本地文件路径已经存在无需创建")
        else:
            print("本地路径不存在,开始创建······")
            os.mkdir(dir_name)
    def file_is_exist(self,file_name):
        if os.path.exists(file_name):
            print("本地文件已经存在,开始删除文件")
            os.remove(file_name)
        else:
            print("文件不存在,开始写入数据")
    def download_file(self):
        _buffer_size = 102400
        for ip in self.config.FTP_HOST:
            ftp = self.ftp_connect(proble_ip=ip)
            print(ftp.getwelcome())
            #检测ftp文件路径是否存在
            if os.path.isdir(self.config.FTP_DIR):
                print("FTP路径存在开始读取文件")
                #检测写入文件路径是否存在,不存在就创建
                self.create_dir(dir_name=self.config.LOCAL_DIR)
                for name in self.config.FILE_MAP.keys():
                    yesterday_file_dir = self.config.FTP_DIR + "/" + name + "/" + self.yesterday
                    print("开始读取文件数据,您将读取的文件路径为 " + yesterday_file_dir)
                    file_list = ftp.nlst(yesterday_file_dir)
                    write_file = self.config.LOCAL_DIR + "/" + name + self.yesterday + ".txt"
                    print("您即将写入的文件为:" + write_file)
                    # 检测文件是否存在,如果存在就删除
                    self.file_is_exist(file_name=write_file)
                    for file_name in file_list:
                        ftp_file = os.path.join(self.config.FTP_DIR,file_name)
                        with open(write_file,"ab+") as f:
                            ftp.retrbinary('RETR %s' %ftp_file,f.write,_buffer_size)
                            f.close()
            else:
                print(ip + self.config.FTP_DIR + "探针数据路径不存在,退出,进行下个服务器的采集")
                continue
            ftp.quit()

class Config(object):
    FTP_HOST = ("*****","*****")
    FTP_USER = "*****"
    FTP_PASSWORD = "****"
    FTP_PORT = 21
    FTP_DIR = "/"
    LOCAL_DIR = "*******"
    FILE_MAP ={
        "****":1001,
        "****":1002,
        "*****":1003,
    }

if __name__ == '__main__':
    ftpget = FtpGet()
    ftpget.download_file()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值