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)