Python一键部署文件

第一步:先从git clone 文件

https://www.cnblogs.com/wanglan/p/10718876.html#autoid-2-0-0

from git import Repo

#
to_path ="E:\zsyec"
url='***.git'
#git clone
clone = Repo.clone_from(url,to_path)

 

第二步:文件压缩

 

import os
import zipfile

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()

if __name__ == "__main__":
    startdir = "E:\econtract\src"  #要压缩的文件夹路径
    file_news = startdir +'.zip' # 压缩后文件夹的名字
    zip_ya(startdir,file_news)

 

第三步:上传文件

import paramiko

hostname = '10.XX.XX.XX'
username = 'XXXX'
password = 'XXXX'
port = 22
pkey = 'D:\XX'

def password_upload(local_dir, remote_dir):
    transport = paramiko.Transport((hostname, port))
    transport.connect(username= 'XX', password='XX')
    sftp = paramiko.SFTPClient.from_transport(transport)  # 如果连接需要密钥,则要加上一个参数,hostkey="密钥"
    sftp.put(local_dir, remote_dir)  # 将本地的Windows.txt文件上传至服务器/root/Windows.txt
    transport.close()  # 关闭连接

 

posted @ 2019-05-14 18:37  闪电旅途  阅读(617)  评论(0编辑  收藏  举报