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 pythonimport 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 实现远程上传文件夹 的全部内容, 来源链接: utcz.com/z/387719.html