SFTP,縮寫為 SSH 文件傳輸協議,被稱為安全文件傳輸協議,是一種網絡協議,允許我們通過任何可靠的數據流訪問、傳輸和管理文件。該程序在安全通道上工作,如 SSH,服務器已經對客戶端進行了身份驗證,並且客戶端用戶的身份可用於該協議。
如果我們使用像 Python 這樣的編程語言,那麼使用 SFTP 處理腳本總是可以支持的。Python 提供了 pysftp 模塊,讓我們可以輕鬆高效地處理這項技術。
在下面的教程中,我們將通過一些例子來了解 pysftp 模塊及其在 Python 編程語言中的使用。
所以,讓我們開始吧。
了解 Python pysftp
模塊
Python pysftp 模塊是一個簡單的 sftp 接口。該模塊提供高級抽象和基於任務的時間表,以便處理 SFTP 需求。SFTP 協議不支持安全性和身份驗證;它期待基本協議來保護它。SFTP 被最廣泛地用作 SSH 協議第 2 版的子系統實現,由同一個工作組設計。
pysftp 模塊作為 Paramiko 的包裝器,具有更受 Python 影響的界面。 Paramiko 庫是最棒的 python 庫之一,被認為是 pysftp 的主幹。由 pysftp 創建的方法是抽象的,通過封裝與 sftp 交互的各種更高功能的用例來服務於程序員的生產力。而不是寫腳本走目錄,調用 get 和 put,不僅要處理 Python 的 Paramiko 還要處理 OS 和 stat 模塊,還要編寫測試(網上很多代碼片段都是不完整的,不考慮邊緣情況)。 pysftp 模塊提供了一個完整的庫來處理這三個。讓我們專註於其他主要任務。
現在,讓我們安裝 Python SFTP 模塊或 pysftp。
如何安裝 pysftp
模塊?
pysftp 界面沒有暴露 Paramiko 的大部分特性;然而,它使用單一方法抽象出了相當多的特徵。相比之下, pysftp 模塊在 Paramiko、之上實現了更多高級功能,特別是遞歸文件傳輸。
我們可以使用以下命令在 pip 安裝程序的幫助下安裝 pysftp 模塊:
語法:
$ pip install pysftp
# or
$ python3 -m pip install pysftp
該模塊將作為 Python 和 pip 版本安裝在系統中。
驗證安裝
為了檢查模塊是否已經正確安裝在系統中,我們可以嘗試導入模塊並執行程序。
安裝完成後,創建一個新的 Python 文件,並在其中鍵入以下語法。
示例:
# importing the required module
import pysftp
現在,保存該文件並在命令提示符下使用以下命令運行該文件。
語法:
$ python <file-name>.py
如果程序運行時沒有出現任何導入錯誤,則模塊安裝正確。否則,建議重新安裝該模塊,並參考其官方文檔。
使用 pysftp 訪問 SFTP 服務器
我們可以藉助 Python 中的 pysftp 模塊列出方向的內容。為了實現這個目標,我們需要主機名、用戶名和密碼。
然後我們必須使用 chdir 或 cwd 方法切換方向,並提供第一個參數作為遠程目錄。
讓我們同樣考慮下面的例子。
示例:
# importing the required module
import pysftp
# defining the host, username, and password
my_Hostname = "welcomeblog.com"
my_Username = "root"
my_Password = "root"
# using the python with statement
with pysftp.Connection(
host = my_Hostname,
username = my_Username,
password = my_Password
) as sftp:
print("Connection succesfully established ... ")
# Switching to a remote directory
sftp.cwd('/var/www/vhosts/')
# Obtaining structure of the remote directory '/var/www/vhosts'
dir_struct = sftp.listdir_attr()
# Printing data
for attr in dir_struct:
print(attr.filename, attr)
# connection closed automatically at the end of the with statement
說明:
上面的代碼片段是一個不存在的虛擬服務器。然而,在現實生活中,出於安全目的,我們必須利用環境變量來獲取任何文件中的原始憑據,而不是提取單個文件中的所有憑據。建議將其放在環境變量文件中。例如。env 文件。
現在,讓我們理解上面的代碼。上面的代碼片段對任何人來說都是一樣的,因為我們必須提供憑據,並且程序將開始工作。
首先,我們導入了 pysftp 模塊,然後提供變量來存儲主機名、用戶名和密碼的值。然後,我們使用 Python 和語句,通過提供主機名、用戶名和密碼來打開到遠程服務器的安全連接。如果成功,我們將切換遠程目錄,獲取列表並在控制台中逐一打印。
該列表是按任意順序排列的,它不涉及唯一條目“.”和“..”。返回的 SFTPAttributes 對象將具有附加字段: longname,,該字段可能由 UNIX 格式的文件屬性的格式化字符串組成。字符串的內容將依賴於 SFTP 服務器。
在 Python 中使用 pysftp 上傳文件
我們可以使用 sftp 客戶端的 sftp.put() 功能,藉助 pysftp 通過 SFTP 上傳遠程服務器中的文件。 put 方法期望我們需要上傳的文件的相對或絕對本地路徑作為第一個參數,文件應該上傳的遠程路徑作為第二個參數。
為了更好地理解,讓我們考慮下面的代碼片段。
示例:
# importing the required module
import pysftp
# defining the host, username, and password
my_Hostname = "welcomeblog.com"
my_Username = "root"
my_Password = "root"
# using the python with statement
with pysftp.Connection(
host = my_Hostname,
username = my_Username,
password = my_Password
) as sftp:
print("Connection succesfully established ... ")
# Defining a file that we want to upload from the local directorty
# or absolute "/Users/krunal/Desktop/code/pyt/app.txt"
local_File_Path = './app.txt'
# Defining the remote path where the file will be uploaded
remote_File_Path = '/var/backups/app.txt'
# Using the put method to upload a file
sftp.put(local_File_Path, remote_File_Path)
# connection closed automatically at the end of the with statement
說明:
在上面的代碼片段中,我們已經建立了安全連接,然後定義了兩個文件路徑- 本地 文件 路徑和遠程 文件 路徑。
- local_File_Path: 此文件路徑是指向本地文件的路徑。
- remote_File_Path: 此文件路徑是指向遠程文件的路徑。
然後,我們使用 sftp.put() 函數將文件上傳到服務器。
使用 Python 中的 pysftp 下載遠程文件
在前一節中,我們已經討論了使用 pysftp 上傳文件的方法。讓我們了解一下下載文件的方法。
我們可以在 pysftp 的幫助下,通過打開一個連接,從 sftp 實例,並利用 get 方法,從服務器下載一個遠程文件,該方法需要一個將要下載的遠程文件的路徑。第二個參數是我們應該存儲文件的本地路徑。
讓我們考慮下面的例子來證明這一點。
示例:
# importing the required module
import pysftp
# defining the host, username, and password
my_Hostname = "welcomeblog.com"
my_Username = "root"
my_Password = "root"
# using the python with statement
with pysftp.Connection(
host = my_Hostname,
username = my_Username,
password = my_Password)
as sftp:
print("Connection succesfully established ... ")
# Defining the remote path file path
remote_File_Path = '/var/backups/app.txt'
# Defining a directory in which we have to save the file.
# or absolute "/Users/krunal/Desktop/code/pyt/app.txt"
local_File_Path = './app.txt'
# Using the get method to download a file
sftp.get(remote_File_Path, local_File_Path)
# connection closed automatically at the end of the with statement
說明:
在上面的代碼片段中,我們定義了一個連接,然後定義了兩個文件路徑- 遠程 文件 路徑和本地 文件 路徑
- remote_File_Path: 該文件路徑是文件所在的路徑。
- local_File_Path: 該文件路徑是文件將被下載的路徑。
然後我們利用 sftp.get() 函數來下載文件。
在 Python 中使用 pysftp 刪除文件
我們可以使用 sftp.remove() 功能在 pysftp 的幫助下刪除文件。 remove() 函數需要遠程文件的絕對路徑作為第一個參數。
讓我們考慮下面的例子來證明這一點。
示例:
# importing the required module
import pysftp
# defining the host, username, and password
my_Hostname = "welcomeblog.com"
my_Username = "root"
my_Password = "root"
# using the python with statement
with pysftp.Connection(
host = my_Hostname,
username = my_Username,
password = my_Password)
as sftp:
print("Connection succesfully established ... ")
# Defining the remote path file path
remote_File_Path = '/var/backups/app.txt'
# Using the get method to download a file
sftp.remove(remote_File_Path)
# connection closed automatically at the end of the with statement
說明:
在上面的代碼片段中,我們已經打開了一個連接,然後定義了一個 remove_File_Path 變量,該變量由一個需要刪除的文件的路徑組成。
然後,我們利用 sftp.remove() 功能從遠程服務器刪除一個文件。
pysftp 模塊有多種多樣的功能,我們可以利用它們來執行各種活動,例如處理權限等等。也可以參考 Python pysftp 模塊的官方文檔。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/284939.html