python下载文件传到服务器_windows上python上传下载文件到linux服务器指定路径【转】...

#!/usr/bin/env python#coding: utf-8

importparamikoimportdatetimeimportos

hostname='10.xxx.xxx.xx'username='username'password='***'port=22

defupload(local_dir,remote_dir):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 inos.walk(local_dir):print('[%s][%s][%s]' %(root,dirs,files))for filespath infiles:

local_file=os.path.join(root,filespath)print(11,'[%s][%s][%s][%s]' %(root,filespath,local_file,local_dir))

a= local_file.replace(local_dir,'').replace('\\','/').lstrip('/')print('01',a,'[%s]' %remote_dir)

remote_file=os.path.join(remote_dir,a)print(22,remote_file)try:

sftp.put(local_file,remote_file)exceptException as e:

sftp.mkdir(os.path.split(remote_file)[0])

sftp.put(local_file,remote_file)print("66 upload %s to remote %s" %(local_file,remote_file))for name indirs:

local_path=os.path.join(root,name)print(0,local_path,local_dir)

a= local_path.replace(local_dir,'').replace('\\','')print(1,a)print(1,remote_dir)

remote_path=os.path.join(remote_dir,a)print(33,remote_path)try:

sftp.mkdir(remote_path)print(44,"mkdir path %s" %remote_path)exceptException as e:print(55,e)print('77,upload file success %s' %datetime.datetime.now())

t.close()exceptException as e:print(88,e)if __name__=='__main__':

local_dir=r'D:\111'remote_dir='/home/share/111/'upload(local_dir,remote_dir)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值