
- Python - 網路程式設計
- Python - 網路基礎
- Python - 網路環境
- Python - 網際網路協議
- Python - IP 地址
- Python - DNS 查詢
- Python - 路由
- Python - HTTP 請求
- Python - HTTP 響應
- Python - HTTP 頭部
- Python - 自定義 HTTP 請求
- Python - 請求狀態碼
- Python - HTTP 認證
- Python - HTTP 資料下載
- Python - 連線重用
- Python - 網路介面
- Python - 套接字程式設計
- Python - HTTP 客戶端
- Python - HTTP 伺服器
- Python - 構建 URL
- Python - Web 表單提交
- Python - 資料庫和 SQL
- Python - Telnet
- Python - 郵件訊息
- Python - SMTP
- Python - POP3
- Python - IMAP
- Python - SSH
- Python - FTP
- Python - SFTP
- Python - Web 伺服器
- Python - 上傳資料
- Python - 代理伺服器
- Python - 目錄列表
- Python - 遠端過程呼叫
- Python - RPC JSON 伺服器
- Python - Google 地圖
- Python - RSS Feed
Python - FTP
FTP 或檔案傳輸協議是一種眾所周知的網路協議,用於在網路中的計算機之間傳輸檔案。它建立在客戶端伺服器架構上,可以與使用者身份驗證一起使用。它也可以在沒有身份驗證的情況下使用,但這將不太安全。FTP 連線維護當前工作目錄和其他標誌,並且每次傳輸都需要一個輔助連線來傳輸資料。大多數常見的 Web 瀏覽器可以檢索托管在 FTP 伺服器上的檔案。
FTP 類中的方法
在 Python 中,我們使用模組ftplib,它具有以下列出檔案所需的方法,因為我們將傳輸檔案。
方法 | 描述 |
---|---|
pwd() | 當前工作目錄。 |
cwd() | 將當前工作目錄更改為路徑。 |
dir([path[,...[,cb]]) | 顯示路徑的目錄列表。可選回撥 cb 傳遞給 retrlines()。 |
storlines(cmd, f) | 使用給定的 FTP 命令上傳文字檔案 - 例如,STOR 檔名。 |
storbinary(cmd,f[, bs=8192]) | 類似於 storlines(),但用於二進位制檔案。 |
delete(path) | 刪除位於路徑的遠端檔案。 |
mkd(directory) | 建立遠端目錄。 |
異常 ftplib.error_temp | 當收到表示臨時錯誤的錯誤程式碼(響應程式碼在 400–499 範圍內)時引發異常。 |
異常 ftplib.error_perm | 當收到表示永久錯誤的錯誤程式碼(響應程式碼在 500–599 範圍內)時引發異常。 |
connect(host[, port[, timeout]]) | 連線到給定的主機和埠。預設埠號為 21,如 FTP 協議所指定。 |
quit() | 關閉連線並退出。 |
以下是上述某些方法的示例。
列出檔案
以下示例使用匿名登入到 ftp 伺服器並列出當前目錄的內容。它遍歷檔案和目錄的名稱並將它們儲存為列表。然後打印出來。
import ftplib ftp = ftplib.FTP("ftp.nluug.nl") ftp.login("anonymous", "ftplib-example-1") data = [] ftp.dir(data.append) ftp.quit() for line in data: print "-", line
當我們執行上述程式時,我們得到以下輸出 -
- lrwxrwxrwx 1 0 0 1 Nov 13 2012 ftp -> . - lrwxrwxrwx 1 0 0 3 Nov 13 2012 mirror -> pub - drwxr-xr-x 23 0 0 4096 Nov 27 2017 pub - drwxr-sr-x 88 0 450 4096 May 04 19:30 site - drwxr-xr-x 9 0 0 4096 Jan 23 2014 vol
更改目錄
以下程式使用 ftplib 模組中提供的 cwd 方法更改目錄,然後獲取所需內容。
import ftplib ftp = ftplib.FTP("ftp.nluug.nl") ftp.login("anonymous", "ftplib-example-1") data = [] ftp.cwd('/pub/') change directory to /pub/ ftp.dir(data.append) ftp.quit() for line in data: print "-", line
當我們執行上述程式時,我們得到以下輸出 -
- lrwxrwxrwx 1 504 450 14 Nov 02 2007 FreeBSD -> os/BSD/FreeBSD - lrwxrwxrwx 1 504 450 20 Nov 02 2007 ImageMagick -> graphics/ImageMagick - lrwxrwxrwx 1 504 450 13 Nov 02 2007 NetBSD -> os/BSD/NetBSD - lrwxrwxrwx 1 504 450 14 Nov 02 2007 OpenBSD -> os/BSD/OpenBSD - -rw-rw-r-- 1 504 450 932 Jan 04 2015 README.nluug - -rw-r--r-- 1 504 450 2023 May 03 2005 WhereToFindWhat.txt - drwxr-sr-x 2 0 450 4096 Jan 26 2008 av - drwxrwsr-x 2 0 450 4096 Aug 12 2004 comp
獲取檔案
如上所示,在獲取檔案列表後,我們可以使用getfile方法獲取特定檔案。此方法將檔案的副本從遠端系統移動到啟動 ftp 連線的本地系統。
import ftplib import sys def getFile(ftp, filename): try: ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write) except: print "Error" ftp = ftplib.FTP("ftp.nluug.nl") ftp.login("anonymous", "ftplib-example-1") ftp.cwd('/pub/') change directory to /pub/ getFile(ftp,'README.nluug') ftp.quit()
當我們執行上述程式時,我們發現檔案 README.nlug 存在於啟動連線的本地系統中。
廣告