1. 程式人生 > 實用技巧 >上傳檔案到windows server, 匯出檔案到linux, 打包壓縮

上傳檔案到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