from ftplib import FTP
from ftplib import FTP_TLS # 如果 FTP server 有支援加密連線
...
...
# 定義 FTP 連線資訊
sFTPhost = 'IP 或 網址'
sFTPport = 20 # 本例因使用預設的 port,故不會用到
sFTPuser = '帳號'
sFTPpwd = '密碼'
# 下載的檔案要存放的位置
local_path = "/case/python/download"
ftp = FTP(sFTPhost, sFTPuser, sFTPpwd)
ftp.cwd("download") # FTP server 端切換至指定的目錄
lstFTPfile = ftp.nlst() # 列出所有檔案
for sFTPfile in lstFTPfile:
# 過濾要下載的檔案,在本例,只下載檔名中有 ".ZIP" 的檔案
if ('.ZIP' in sFTPfile):
localfile = open(local_path+'/'+sFTPfile,'wb') # 本地端要儲存的檔案
ftp.retrbinary('RETR ' + sFTPfile, localfile.write) # 下載檔案
localfile.close()
ftp.quit # 結束連線
沒有留言:
張貼留言