python 实现远程上传文件夹

 

python2 upload.py "ip" "root" "password" "22" "Only Project DLL" "d:\ENZO\Publish\Wxcrm.Admin\bin\release\netcoreapp2.1\centos.7-x64\publish" "/mondao/deployment/wxcrm-admin" 

#!/usr/bin/env python
import sys, time
import paramiko,datetime,os

hostname=''
username='root'
password=''
port=22

def upload(local_dir, remote_dir, special_names):
    try:
        t=paramiko.Transport((hostname, port))
        t.connect(username=username,password=password)
        sftp=paramiko.SFTPClient.from_transport(t)
        print 'upload file start %s ' % datetime.datetime.now()
        for root,dirs,files in os.walk(local_dir):
            for name in dirs:
                local_path = os.path.join(root, name)
                a = local_path.replace(local_dir,'').replace('\\', '/')
                remote_path = remote_dir + a
                try:
                    sftp.mkdir(remote_path)
                    print "mkdir path %s" % remote_path
                except Exception,e:
                    print("dir path is exists: %s") % remote_path
                    
            for filespath in files:
                local_file = os.path.join(root,filespath)
                a = local_file.replace(local_dir,'').replace('\\', '/')
                remote_file = remote_dir + a
                isSpecialFile = len(special_names)==0
                if not isSpecialFile:
                    for special_name in special_names:
                        if special_name in local_file:
                            isSpecialFile = True
                if isSpecialFile:
                    try:
                        sftp.put(local_file, remote_file)
                    except Exception,e:
                        sftp.mkdir(os.path.split(remote_file)[0])
                        sftp.put(local_file, remote_file)
                    print "upload %s to remote %s" % (local_file, remote_file)
        print 'upload file success %s ' % datetime.datetime.now()
        t.close()
    except Exception,e:
        print e

if __name__=='__main__':
    args = sys.argv[1:]
    if not args:
        print("not args")
        sys.exit(1);
        
    hostname = args[0]
    username = args[1]
    password = args[2]
    port = int(args[3])
    type = args[4]
    local_dir = args[5]
    remote_dir = args[6]

    print("Paramaters:")
    print("hostname: " + hostname)
    print("username: " + username)
    print("password: " + password)
    print("port: " + str(port))
    print("type: " + type)
    print("local_dir: " + local_dir)
    print("remote_dir: " + remote_dir)
    
    t = time.time()
    
    special_names = []
    if type == "Project":
        special_names = ["Wxcrm.Admin.", "wwwroot\js", "\config"]
    elif type == "Only Project DLL":
        special_names = ["Wxcrm.Admin."]
    elif type == "Only Project JS":
        special_names = ["wwwroot\js"]
    else:
        special_names = []
    #local_dir=r'd:\ENZO\Publish\Wxcrm.Admin\bin\release\netcoreapp2.1\centos.7-x64\publish'
    #remote_dir=r'/mondao/deployment/wxcrm-admin'
    upload(local_dir, remote_dir, special_names)

    print("total run time:")
    e = time.time()
    print(e-t)

 

转载于:https://www.cnblogs.com/dfg727/p/9591901.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值