Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding:utf-8
- """ 下载任务监控与文件上传
- Detect the specified-type files in the folder which finished downloading and transfer them to FTP server.
- """
- # ----------------------------
- # Author: Eli, Kun Liu
- # Start date: 2017-04
- # Latest edit: 2017-05-22
- # -----------------------------
- __python__ = 2.7
- import os
- import threading
- import shutil
- import sys
- import time
- from ftplib import FTP
- ISOTIMEFORMAT = '%Y-%m-%d %X'
- sizeWritten = 0
- totalSize = 0
- TARGET_DIR_NAME = settings.TARGET_DIR_NAME
- THREAD_NUM = 4
- UPLOAD_BUFSIZE = 8192
- # 根据后缀筛选文件
- legal_url_extensions = ('.dmg', '.pkg', '.sit', '.hqx', '.sitx', '.dmg.gz', '.dmg.sit', '.zip')
- def ftp_login():
- ftp_host_address = settings.ADDRESS
- ftp_port = settings.PORT
- ftp_username = settings.USERNAME
- ftp_pass = settings.PASSWORDS
- ftp = FTP()
- ftp.set_debuglevel(0)
- ftp.connect(ftp_host_address, ftp_port)
- ftp.login(ftp_username, ftp_pass)
- try:
- ftp.cwd('/%s'%TARGET_DIR_NAME)
- except Exception, ex:
- print "[ERROR] " + str(ex)
- sys.exit()
- return ftp
- def upload_callback(buf):
- global sizeWritten
- global totalSize
- sizeWritten += len(buf)
- status = r"%10d [%3.2f%%]" % (sizeWritten, sizeWritten * 100.0 / totalSize)
- print status,
- print (chr(8)) * (len(status) + 2),
- def get_current_dir_filenames(ftp):
- lines = []
- ftp.dir(lines.append)
- filenames = []
- for line in lines:
- part = line.split(' ')[-1]
- filenames.append(part)
- return filenames
- def upload_to_ftp(local_file):
- global sizeWritten
- global totalSize
- ftp = ftp_login()
- print "****************************New Start**********************************"
- print time.strftime(ISOTIMEFORMAT, time.localtime())
- print "Uploading %s to Ftp. Please wait......."%local_file.split("/")[-1]
- bufsize = UPLOAD_BUFSIZE
- if not os.path.isfile(local_file):
- return
- file_handler = open(local_file, "rb")
- totalSize += os.path.getsize(local_file)
- sizeWritten = 0
- ftp.storbinary('STOR %s' % os.path.basename(local_file), file_handler, bufsize, callback=upload_callback)
- file_handler.close()
- print "\t[Success]" + local_file + " Upload into FTP Success!"
- ftp.quit()
- def extract_file(local_dir):
- for root, dirs, files in os.walk(local_dir):
- for f in files:
- if f.endswith(legal_url_extensions):
- file_path = os.path.join(root, f)
- try:
- upload_to_ftp(file_path)
- except Exception, ex:
- print "[ERROR in UPLOAD] " + str(ex)
- else:
- os.remove(file_path)
- print "delete local files"
- shutil.rmtree(local_dir)
- def upload_files_dirs(local_dir):
- if os.path.isdir(local_dir): # if it is dir , upload it in another program .
- extract_file(local_dir)
- return
- try:
- if local_dir.endswith(legal_url_extensions):
- upload_to_ftp(local_dir)
- except Exception, ex:
- print "[ERROR in UPLOAD] " + str(ex)
- else:
- os.remove(local_dir)
- print "delete local files"
- def monitor_file_from(dir):
- allfiles = os.listdir(dir)
- # 仅对 aria2c 已完成下载的文件或文件夹内容进行上传
- for allfile in allfiles:
- if allfile.endswith('aria2') or allfile.endswith('temp'):
- continue
- tmp_1 = allfile + '.aria2'
- tmp_2 = allfile + '.aria2__temp'
- if tmp_1 in allfiles or tmp_2 in allfiles:
- continue
- local_dir = dir + allfile
- upload_files_dirs(local_dir)
- def timer_task():
- print "**************************Enter in Timer task ************************** "
- threads_list = []
- dir_list = ['/root/remote_transfor/']
- for dir in dir_list:
- monitor_file_from(dir)
- print "**************************Get out Timer task ************************** "
- global t
- t = threading.Timer(10, timer_task)
- t.start()
- t = threading.Timer(10, timer_task)
- def main():
- global t
- t.start()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement