python 操作Windows文件上傳Linux,Linux文件下載到Windows本地

從Windows上傳文件到linux目錄下,文件夾自動創建

[python] view plain copy

  1. import paramiko  
  2. import datetime 
  3. import os  
  4.  
  5. hostname='10.xxx.xxx.xx'  
  6. username='username'  
  7. password='***'  
  8. port=22  
  9.   
  10. def upload(local_dir,remote_dir):  
  11.     try:  
  12.         t=paramiko.Transport((hostname,port))  
  13.         t.connect(username=username,password=password)  
  14.         sftp=paramiko.SFTPClient.from_transport(t)  
  15.         print('upload file start %s ' % datetime.datetime.now())  
  16.         for root,dirs,files in os.walk(local_dir):  
  17.             print('[%s][%s][%s]' % (root,dirs,files))  
  18.             for filespath in files:  
  19.                 local_file = os.path.join(root,filespath)  
  20.                 print(11,'[%s][%s][%s][%s]' % (root,filespath,local_file,local_dir))  
  21.                 a = local_file.replace(local_dir,'').replace('\\','/').lstrip('/')  
  22.                 print('01',a,'[%s]' % remote_dir)  
  23.                 remote_file = os.path.join(remote_dir,a)  
  24.                 print(22,remote_file)  
  25.                 try:  
  26.                     sftp.put(local_file,remote_file)  
  27.                 except Exception as e:  
  28.                     sftp.mkdir(os.path.split(remote_file)[0])  
  29.                     sftp.put(local_file,remote_file)  
  30.                     print("66 upload %s to remote %s" % (local_file,remote_file))  
  31.             for name in dirs:  
  32.                 local_path = os.path.join(root,name)  
  33.                 print(0,local_path,local_dir)  
  34.                 a = local_path.replace(local_dir,'').replace('\\','')  
  35.                 print(1,a)  
  36.                 print(1,remote_dir)  
  37.                 remote_path = os.path.join(remote_dir,a)  
  38.                 print(33,remote_path)  
  39.                 try:  
  40.                     sftp.mkdir(remote_path)  
  41.                     print(44,"mkdir path %s" % remote_path)  
  42.                 except Exception as e:  
  43.                     print(55,e)  
  44.         print('77,upload file success %s ' % datetime.datetime.now())  
  45.         t.close()  
  46.     except Exception as e:  
  47.         print(88,e)  
  48.           
  49. if __name__=='__main__':  
  50.      local_dir=r'D:\test'  
  51.      remote_dir='/home/share/test/'  
  52.      upload(local_dir,remote_dir)  

 

使用python操作從linux服務器下載文件到Windows指定路徑下

[python] view plain copy

  1. import paramiko  
  2. def remote_scp(host_ip,remote_path,local_path,username,password):  
  3.     t = paramiko.Transport((host_ip,22))  
  4.     t.connect(username=username, password=password)  
  5.     sftp = paramiko.SFTPClient.from_transport(t)  
  6.     src = remote_path  
  7.     des = local_path  
  8.     sftp.get(src,des)  
  9.     t.close()  
  10.  
  11. if __name__ == '__main__':  
  12.     host_ip = '10.xxx.xxx.xx'  
  13.     remote_path = '/home/share/a.pdf'  
  14.     local_path = r'D:\aaa\a.pdf1'  
  15.     username = 'username'  
  16.     password = '******'  
  17.     remote_scp(host_ip,remote_path,local_path,username,password)  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章