2022-08-24

【Python2】FTP 客戶端應用

不確定是不是版本限制,Python2.7 無法加密連線。
 
  1.  
  2. #!/usr/bin/python
  3. #-*- coding:utf-8 -*-
  4.  
  5. import sys
  6.  
  7. # FTP
  8. from ftplib import FTP
  9.  
  10. sFTPhost = '主機網址/IP'
  11. sFTPuser = '帳號'
  12. sFTPpwd = '密碼'
  13.  
  14.  
  15. ftp = FTP(sFTPhost, sFTPuser, sFTPpwd)
  16. # ftp = FTP_TLS(sFTPhost, sFTPuser, sFTPpwd)
  17. ftp.cwd("/dir1/dir11") # 切換在 server 的目錄
  18. lstFTPfile = ftp.nlst() # 列出所有檔案
  19. for sFTPfile in lstFTPfile:
  20. print 'sFTPfile = ' + sFTPfile
  21. localfile = open(sFTPfile,'wb') # 本地端要儲存的檔案
  22. ftp.retrbinary('RETR ' + sFTPfile, localfile.write) # 下載檔案
  23. localfile.close()
  24.  
  25.  
  26. # 上傳檔案
  27. localfile = open(sFTPfile,'rb')
  28. ftp.cwd("/dir1/dir12") # 切換在 server 的目錄
  29. ftp.storbinary('STOR '+sFTPfile, localfile)
  30.  
  31. break
  32.  
  33. ftp.quit
  34.  


沒有留言:

張貼留言