python 压缩文件并上传文件

#! /usr/bin/env python
# -*- coding:utf-8 -*-

import os
#zip压缩模块
import zipfile
#上传模块
import paramiko

remoteIp = '192.168.1.100'
remotePort = '22'
remoteUser = 'root'
remotePw = '123456'

def Compress(dirs, path):
    kZip = zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED)
    for dir in dirs:
        for dirpath, dirnames, filenames in os.walk(dir):
            for file in filenames:
                kZip.write(os.path.join(dirpath,file))    #绝对路径
    kZip.close()
    print('compress finished')
    
def Upload_file(local_path, remote_path):
    
    try:
        kParamiko = paramiko.Transport(remoteIp,remotePort)
        print('Transport Success!')
        kParamiko.connect(username = remoteUser, password = remotePw)
        print('connect Success!')
        kFtp = paramiko.SFTPClient.from_transport(kParamiko)
        print('from_transport Success!')
        local_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), local_path)
        print local_path
        kFtp.put(localpath=local_path,remotepath=remote_path)
        print('upload ' + local_path + ' Success!')
    except Exception as e:
        print(e)
    finally:
      kFtp.close()
      print("done")
      
files = ['./resource']
local_path = "resource.zip"
remote_path = "/prj/git_dev/release/game/" + local_path
Compress(files, local_path)
Upload_file(local_path,remote_path)
 

你可能感兴趣的:(python)