FTP数据上传与目标文件监控、打印实时上传进度、创建当天日期

FTP数据上传与目标文件监控、打印实时上传进度、创建当天日期

项目场景:

提示:
FTP数据上传与目标文件监控、打印实时上传进度、


问题描述:

在ftp数据上传和目标文件监控的问题上,我们有很多文章可以参考,但是几乎都是一个模板,新手不知道如何自己加新功能,这个文章就是包含了很多比较实用的功能。包括目标文件监控,实时文件上传,打印上传进度。对上传文件进行记录,记录上传时间,上传文件名称,上传文件大小,上传类型(与后期断电续传区别)。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import json
import logging
import datetime
import pyinotify
from ftplib import FTP
sys.path.append('..')
from config import read_config

__log_format = "%(asctime)s-%(process)d-%(thread)d - %(name)s:%(module)s:%(funcName)s - %(levelname)s - %(message)s"
logging.basicConfig(level=logging.INFO, format=__log_format)

#打印实时上传进度
upload_size = 0
def upload_file_cb(block):
    global upload_size
    upload_size = upload_size + len(block)
    file_name = local_name.split("/")[-1]
    print("\033[36m\r文件{}传输完成 {}%\033[0m".format(file_name,round((upload_size/file_size)*100),5), end="")

#上传文件,将文件信息记录在log文件中
def uploadfile(ftp, remotepath, localpath):
        buf_size = 4096
        global file_size
        fp = open(localpath, "rb")
        file_size = os.path.getsize(localpath)
        ftp.storbinary("STOR {}".format(remotepath), fp, buf_size,upload_file_cb)
        now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        type = "实时传输"
        if os.path.exists(load_path + "/" +'upload_log.txt'):
            pass
        else:
            with open(load_path + "/" +"upload_log.txt", mode='w', encoding='utf-8') as ff:
                logging.info("\033[36m文件创建成功!\033[0m")
        with open(load_path + "/" +"upload_log.txt","a+") as f:
            f.write("{},文件{}已上传,文件大小{},类型:{}\n".format(now_time,remotepath,file_size,type))
        fp.close()

#检查并创建远程端目录结构,设备名/日期名/上传文件
def creat_makdir(ftp,path):
    ftp.cwd(path)
    config = read_config.ReadConfig()
    with open(config.vbox("config_file_path")) as f:
            name = json.loads(f.read())["general"]["dev_No"]
    dir_list = ftp.nlst()
    if name in dir_list:
        dev_path = os.path.join(path,name)
        ftp.cwd(dev_path)
    else:
        ftp.mkd(name)
        dev_path = os.path.join(path,name)
        ftp.cwd(dev_path)
    today = datetime.datetime.now().strftime('%Y-%m-%d')
    dir_list = ftp.nlst()
    if today in dir_list:
        path = os.path.join(path,name,today)
        ftp.cwd(path)
    else:
        ftp.mkd(today)
        path = os.path.join(path,name,today)
        ftp.cwd(path)

#事件触发类,对监视目录的cp、mv、touch工作进行一些列反应
class UpgradeHandle(pyinotify.ProcessEvent):          
    def process_IN_CREATE(self, event):
        global local_name
        logging.info("\033[36m{},{}\033[0m".format(event.path,event.name))
        logging.info("\033[36mCreate file: {}\033[0m".format(os.path.join(event.path,event.name)))
        local_name = os.path.join(event.path,event.name)
        self.config = read_config.ReadConfig()
        ftp = FTP()
        ftp.connect(self.config.remote_device("remote_address"),int(self.config.remote_device("remote_port")))
        ftp.login(self.config.remote_device("remote_name"), self.config.remote_device("remote_password"))
        path = self.config.device("remote_address_path")
        creat_makdir(ftp,path)
        logging.info("\033[36m{},{}\033[0m".format(event.pathname.split("/")[-1], event.pathname))
        uploadfile(ftp,event.pathname.split("/")[-1], event.pathname)
        logging.info("\033[36mfinish\033[0m")

def ServiceRunner(path):
    global load_path
    multi_event = pyinotify.IN_CREATE 
    upload_handle = UpgradeHandle()
    upload_watcher = pyinotify.WatchManager()
    upload_notifier = pyinotify.Notifier(upload_watcher, upload_handle)
    load_path = path
    upload_watcher.add_watch(path, multi_event)
    upload_watcher.add_watch(path, multi_event)
    upload_notifier.loop()

if __name__ == "__main__":   
    ServiceRunner()
#FTP次线程
    today=str(datetime.date.today())
    load_path = str(config.device("local_path")) + "/" + today
    if os.path.exists(load_path):
        pass
    else:
        os.popen("mkdir {}".format(load_path))
    ftp_file = ServiceRunner(load_path)
    ftp_thread = threading.Thread(target=ftp_file)

代码分析:

在代码块中,我们需要创建file_monitor.py,将第一代码块复制进去,在mian.py中复制第二代码块,进行创建FTP线程,如果不需要创建线程,直接调用就好。 如果只是要使用单一代码块一,那么,删除FTP线程代码最后一行,将其他代码代替代码块一最后一行就好了。 代码中的config.device("local_path")、config.remote_device("remote_address")、config.remote_device("remote_port")、config.remote_device("remote_password")、config.remote_device("remote_name")是我自己调用的配置文件,使用的时候直接写成自己的ip、密码、port就好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值