2020-06-05

【Python】FTP client


  1.  
  2. from ftplib import FTP
  3. from ftplib import FTP_TLS # 如果 FTP server 有支援加密連線
  4.  
  5. ...
  6. ...
  7.  
  8. # 定義 FTP 連線資訊
  9. sFTPhost = 'IP 或 網址'
  10. sFTPport = 20 # 本例因使用預設的 port,故不會用到
  11. sFTPuser = '帳號'
  12. sFTPpwd = '密碼'
  13.  
  14. # 下載的檔案要存放的位置
  15. local_path = "/case/python/download"
  16.  
  17. ftp = FTP(sFTPhost, sFTPuser, sFTPpwd)
  18. ftp.cwd("download") # FTP server 端切換至指定的目錄
  19. lstFTPfile = ftp.nlst() # 列出所有檔案
  20. for sFTPfile in lstFTPfile:
  21. # 過濾要下載的檔案,在本例,只下載檔名中有 ".ZIP" 的檔案
  22. if ('.ZIP' in sFTPfile):
  23. localfile = open(local_path+'/'+sFTPfile,'wb') # 本地端要儲存的檔案
  24. ftp.retrbinary('RETR ' + sFTPfile, localfile.write) # 下載檔案
  25. localfile.close()
  26. ftp.quit # 結束連線
  27.  





沒有留言:

張貼留言