注意:Python2.7.18(含) 之後才支援加密連線。
- import sys
- from ftplib import FTP_TLS
- import ssl
- sFTPhost = '主機網址/IP'
- sFTPuser = '帳號'
- sFTPpwd = '密碼'
- # 加密連線
- ftps = FTP_TLS(sFTPhost)
- # ftps.set_debuglevel(2) # To show logs
- ftps.ssl_version = ssl.PROTOCOL_TLSv1_2 # 調整為您自己的加密協定
- ftps.set_pasv(True)
- ftps.encoding = 'utf-8' # 預設是 latin-1 字元, 版本 3.9 之後預設才是 utf-8
- ftps.login(sFTPuser, sFTPpwd)
- ftps.cwd("/dir1/dir11") # 切換在 server 的目錄
- lstFTPfile = ftps.nlst() # 列出所有檔案
- for sFTPfile in lstFTPfile:
- if ('.jpg' in sFTPfile):
- print('sFTPfile = ' + sFTPfile)
- localfile = open(sFTPfile,'wb') # 本地端要儲存的檔案
- ftps.retrbinary('RETR ' + sFTPfile, localfile.write) # 下載檔案
- localfile.close()
- # 上傳檔案
- localfile = open(sFTPfile,'rb')
- ftps.cwd("/dir1/dir12") # 切換在 server 的目錄
- ftps.storbinary('STOR '+sFTPfile, localfile)
- break
- ftps.quit
沒有留言:
張貼留言