zoukankan      html  css  js  c++  java
  • python+fastdfs+nginx实现打包下载功能

        环境介绍:生产服务器开发人员需要给client下发数据,主要是图片及视频;图片服务器用fastdfs,下载由nginx 来提供;

        java 程序来调用此脚本,传递参数来决定打包文件内容;

    #!/usr/bin/env python
    #coding:utf-8
    """修改一些模块,使用python 自带模块;增加日志记录;"""
    import sys,subprocess,os,time,json,shutil,logging
    logname = time.strftime("%Y_%m_%d.log")
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(levelname)s       %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S',
                        filename=logname,
                        filemode='w')
    class Global(object):
        """此脚本用来处理打包文件:传参来决定打包什么文件,并记录打包记录;
        """
        def __init__(self):
            pass
    
        def args_Counts(self):   ##############检测参数的长度
            counts = len(sys.argv)
            if counts == 1 :     ######windows test
                sys.exit(10)
            else:
                if os.path.isfile('argv.txt'):    ######检测这个存在这个文件
                    logging.warning("The argvs file is exist...,WARNING!----------- ")
                    os.remove('argv.txt')
    
                with open("argv.txt",'a+') as fp:
                    for i in sys.argv[1:]:
                        fp.write(i+"
    ")
    
        def args_Change(self):
            file_con = open("argv.txt",'r')
            dirtory = ''
            for i in file_con:
                try:
                    argschange = json.loads(i)        #########解析json
                    key = argschange.keys()[0]        #########获取字典的key,目录的名字
                    name = argschange.get(key)           #########获取字典的values,需要下载的文件名字----> list
                    dest = '/home/fastdfs/storage/data/'      #####新fastdfs 的文件存储路
                except Exception,e:
                    logging.warning("Something is wrong %s----------"%e)
                    sys.exit(10)
                #dest = '/home/fastdfs/storage/files-data/data'        #旧fastdfs
    
                if os.path.isdir(key):
                    logging.warning("The directory %s is exist and will delete it-----------"%key)
                    shutil.rmtree(key)
                    logging.info("The directory %s is not exist and will create it++++++++++"%key)
                    os.mkdir(key)
                else:
                    logging.info("The directory %s is not exist and will create it++++++++++"%key)
                    os.mkdir(key)
    
                if os.path.isfile(key):
                    logging.warning("Error,program is Error----------")
                    sys.exit(20)
                for i,k in enumerate(name):                              ########拷贝部分
                    dest_dir = os.path.dirname(k).split("M00/")[1]      #########切割fastdfs 的文件路径,减少查找时间,精确查找
                    img_name = os.path.basename(k)                       #########合成路径
                    img_dest1 = dest+dest_dir+os.path.sep+img_name       #########最近上传路径
                    img_dest2 = dest+"data/"+dest_dir+os.path.sep+img_name   #########以前上传的路径
                    if os.path.isfile(img_dest1) :     ###############################优化查找部分,并记录
                        logging.info("Find %s++++++++++"%img_name)
                        shutil.copy(img_dest1,key)
                        logging.info("Copy %s %s++++++++++"%(img_dest1,key))
                    else:
                        logging.warning("Not find %s and change directory to find ------"%img_name)
                        if os.path.isfile(img_dest2):
                            logging.info("Find %s++++++++++" % img_name)
                            shutil.copy(img_dest2, key)
                            logging.info("Copy %s %s++++++++++" % (img_dest2, key))
    
                dirtory += key + ' '
            #     # print dirtory
            bdass = time.strftime("%H%M%S")
            zip_name = "bdass" + bdass + ".zip"
            child_zip = subprocess.Popen('zip -r ' + zip_name +" "+ dirtory,shell=True)
            child_zip.wait()
            if os.path.isfile(zip_name):
                logging.info("Compress successfuly++++++++++")
                logging.info("End and Clean workplace++++++++++")
            else:
                logging.warning("Compress failed----------")
                sys.exit(10)
            shutil.move(zip_name,"/usr/local/nginx/html/download/")
            print zip_name
            file_con.seek(0)
            for i in file_con:
                argschange = json.loads(i)        #########解析json
                key = argschange.keys()[0]        #########获取字典的key,目录的名字
                if os.path.isdir(key):
                    shutil.rmtree(key)
    
        def zip_Images(self):  #####结束部分,删除文件
            if os.path.isfile('./argv.txt'):
                os.remove('./argv.txt')
    
    system = Global()
    if __name__ == "__main__":
        logging.info("Begin" + "+" * 20)
        system.args_Counts()
        system.args_Change()
        system.zip_Images()
    

      调用命令:python remote_zip_enhance.py '{"2017092268":["group1/M00/00/00/rB95q1nCGD-AHb-fAAvTKbrr9GI895.png","group1/M00/00/0D/rB95rFm3iuGAYHO2AAjMFXpe4oI406.png","group1/M00/00/04/rB95rFmmja-AVWspAAx1vgabK-I426.png"]}' '{"2017092274":["group1/M00/00/00/rB95q1nCGD-AHb-fAAvTKbrr9GI895.png","group1/M00/00/0D/rB95rFm3iuGAYHO2AAjMFXpe4oI406.png","group1/M00/00/04/rB95rFmmja-AVWspAAx1vgabK-I426.png"]}'

         此前写过基本功能的,有些BUG,打包偶尔有错误;

  • 相关阅读:
    learning scala view collection
    scala
    learning scala dependency injection
    learning scala implicit class
    learning scala type alise
    learning scala PartialFunction
    learning scala Function Recursive Tail Call
    learning scala Function Composition andThen
    System.Threading.Interlocked.CompareChange使用
    System.Threading.Monitor的使用
  • 原文地址:https://www.cnblogs.com/Mail-maomao/p/7587591.html
Copyright © 2011-2022 走看看