
- Python - 網路程式設計
- Python - 網路簡介
- Python - 網路環境
- Python - Internet 協議
- 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 源
Python - 資料上傳
我們可以使用處理 FTP 或檔案傳輸協議的 Python 模組將資料上傳到伺服器。
我們需要安裝模組 ftplib 來實現此目的。
pip install ftplib
使用 ftplib
在下面的示例中,我們使用 FTP 方法連線到伺服器,然後提供使用者憑證。接下來,我們指定檔名稱和 storbinary 方法,以便在伺服器中傳送和儲存檔案。
import ftplib ftp = ftplib.FTP("127.0.0.1") ftp.login("username", "password") file = open('index.html','rb') ftp.storbinary("STOR " + file, open(file, "rb")) file.close() ftp.quit()
當執行上述程式時,我們觀察到已在伺服器中建立了該檔案的副本。
使用 ftpreety
類似於 ftplib,我們可以使用 ftpreety 安全地連線到遠端伺服器並上傳檔案。我們還可以使用 ftpreety 下載檔案。下面的程式說明了這一點。
from ftpretty import ftpretty # Mention the host host = "127.0.0.1" # Supply the credentisals f = ftpretty(host, user, pass ) # Get a file, save it locally f.get('someremote/file/on/server.txt', '/tmp/localcopy/server.txt') # Put a local file to a remote location # non-existent subdirectories will be created automatically f.put('/tmp/localcopy/data.txt', 'someremote/file/on/server.txt')
當執行上述程式時,我們觀察到已在伺服器中建立了該檔案的副本。
廣告