码迷,mamicode.com
首页 > 编程语言 > 详细

python 实现远程上传文件夹

时间:2018-09-05 18:04:43      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:crm   oca   argv   style   publish   []   conf   time()   sys   

 

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)

 

python 实现远程上传文件夹

标签:crm   oca   argv   style   publish   []   conf   time()   sys   

原文地址:https://www.cnblogs.com/dfg727/p/9591901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!