• 上传文件到windows server, 导出文件到linux, 打包压缩


    一. 上传文件到windows server

    import paramiko
    
    def upload_windows(data):
        """
        将execl推到windows_server服务器
        WINDOWS_SERVER_HOST:上传服务器的host
        WINDOWS_SERVER_PORT:上传服务器的port
        WINDOWS_SERVER_USERNAME:上传服务器的username
        WINDOWS_SERVER_PASSWORD:上传服务器的password
        WINDOWS_SERVER_EXCEL:要上传到服务器的文件的路径
        """
        transport = paramiko.Transport(WINDOWS_SERVER_HOST, WINDOWS_SERVER_PORT)
        transport.connect(username=WINDOWS_SERVER_USERNAME, password=WINDOWS_SERVER_PASSWORD)
        sftp = paramiko.SFTPClient.from_transport(transport)
        excl_wind_path = WINDOWS_SERVER_EXCEL
    
        # win_root_file_path = sftp.listdir(WINDOWS_SERVER_EXCEL)
        # print(win_root_file_path)
        #
        # if win_root_file_path == [] or str_uuid not in win_root_file_path:
        #     sftp.mkdir(excl_wind_path + str_uuid)
        # else:
        #     return False
    
        for temp in data:
            sftp.put(temp["local_path"], excl_wind_path + temp["remote_file_name"])
        res_dir = sftp.listdir()
        transport.close()
        return res_dir

    二. 处理上传到windows server中的文件

    def win_excl_sas(export_type):
        """
        export_type:需windows服务器导出的文件的类型
        WINDOWS_SERVER_WINRM_HOST:winrm链接windows的host
        WINDOWS_SERVER_WINRM_USERNAME:winrm链接windows的username
        WINDOWS_SERVER_WINRM_PASSWORD:winrm链接windows的password
        WINDOWS_SERVER_EXCEL_PATH:服务器文件的地址
        
        WINDOWS_SERVER_HOST:windows host
        WINDOWS_SERVER_PORT:windows host
        WINDOWS_SERVER_USERNAME:windows ssh username
        WINDOWS_SERVER_PASSWORD:windows ssh password
        """
        # 执行windows_server中的bat文件, 将execl转成sas
        win2012 = winrm.Session('http://' + WINDOWS_SERVER_WINRM_HOST + '/wsman',
                                auth=(WINDOWS_SERVER_WINRM_USERNAME, WINDOWS_SERVER_WINRM_PASSWORD))
    
        r_open_execl_0 = win2012.run_ps('start ' + WINDOWS_SERVER_EXCEL_PATH + '文件1.xls')   # 执行文件
        r_open_execl_1 = win2012.run_ps('start ' + WINDOWS_SERVER_EXCEL_PATH + '文件2.xls')   # 执行文件
        print(r_open_execl_1, r_open_execl_0)
        if r_open_execl_0.status_code == 0 and r_open_execl_1.status_code == 0:     # 判断文件是否可执行,是否存在未上传完成的情况
            b_kill_execl = win2012.run_ps(r'taskkill /f  /im EXCEL.exe')
        else:
            return 0
        if export_type == "sas":
            run_bat = win2012.run_cmd(WINDOWS_SERVER_BAT)   # 执行windows中的bat文件
        else:
            run_bat = win2012.run_cmd(WINDOWS_SERVER_XPT_BAT)   # 执行windows中的bat文件
    
        if not run_bat:
            return 1
    
        # # 将windwos_server上的sas文件发送到liunx服务器
        transport = paramiko.Transport(WINDOWS_SERVER_HOST, WINDOWS_SERVER_PORT)
        transport.connect(username=WINDOWS_SERVER_USERNAME, password=WINDOWS_SERVER_PASSWORD)
        sftp = paramiko.SFTPClient.from_transport(transport)
    
        # 根据文件类型在linux端新建存储目录
        if export_type == "sas":
            sas_wind_path = WINDOWS_SERVER_SAS
            local_path = current_app.root_path + '/tmp/' + get_time_file() + "sas_file_" + str(uuid.uuid1()) + "/sas_file/"
        else:
            sas_wind_path = WINDOWS_SERVER_XPT
            local_path = current_app.root_path + '/tmp/' + get_time_file() + "xpt_file_" + str(uuid.uuid1()) + "/xpt_file/"
    
        file_lis = sftp.listdir(sas_wind_path)      # 要导出文件夹内所有的文件内容
        for file_p in file_lis:
            print("local_path===========")
            print(local_path)
            print(file_p)
    
            if not os.path.exists(local_path):
                os.makedirs(local_path)
            aa = sftp.get(sas_wind_path + file_p, local_path + file_p)  # 将windows_server上的内容下载到liunx
        transport.close()
    
        # 删除windows server中所有处理过的文件
        del_xpt = 0
        del_execl = win2012.run_ps('del ' + WINDOWS_SERVER_EXCEL_PATH + '*.*')
        del_sas = win2012.run_ps('del ' + WINDOWS_SERVER_SAS_PATH + '*.*')
        if export_type == "xpt":
            del_xpt = win2012.run_ps('del ' + WINDOWS_SERVER_XPT_PATH + '*.*').status_code
    
        if del_execl.status_code != 0 or del_sas.status_code != 0 or del_xpt != 0:
            return 2
        return local_path

    三. 文件打包压缩

    def zipDir(dirpath, outFullName):
        """
        压缩指定文件夹
        :param dirpath: 目标文件夹路径
        :param outFullName: 压缩文件保存路径+xxxx.zip
        :return: 无
        """
        zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED)
        for path, dirnames, filenames in os.walk(dirpath):
            # 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
            fpath = path.replace(dirpath, '')
            for filename in filenames:
                zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
        zip.close()
        return outFullName
  • 相关阅读:
    数据库专题(SQLServer、MySQL)
    第九节:IdentityServer4简介及客户端模式和用户密码模式的应用
    第八节:理解认证和授权、Oauth2.0及四种授权模式、OpenId Connect
    第七节:基于Ocelot网关层的微服务校验(手写jwt校验中间件和利用IdentityModel.Tokens.Jwt校验)
    第六节:Ocelot集成Polly熔断降级,以及缓存、限流、自身负载等其它功能
    第五节:基于Ocelot网关简介、路由功能、集成Consul使用
    第四节:Polly基于控制台和Web端用法(熔断、降级、重试、超时处理等)
    第三节:基于Consul做服务的配置中心
    第二节:Consul简介及服务注册、发现、健康检查
    第一节:微服务准备(Webapi复习、PostMan的使用、项目启动方式、业务代码准备)
  • 原文地址:https://www.cnblogs.com/rixian/p/14167631.html
Copyright © 2020-2023  润新知