python 實現遠程上傳檔案夾

來源:互聯網
上載者:User

標籤: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 pythonimport sys, timeimport paramiko,datetime,oshostname=‘‘username=‘root‘password=‘‘port=22def 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 eif __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 實現遠程上傳檔案夾

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.