Python SFTP

FTP也稱為SSH檔傳輸協議。它是一種網路協議,可通過任何可靠的數據流提供檔訪問,檔傳輸和文件管理。該程式通過安全通道(例如SSH)運行,伺服器已對客戶端進行身份驗證,並且該協議可使用客戶端用戶的身份。

pysftp模組是SFTP的簡單介面。該模組提供高級抽象和基於任務的例程來處理SFTP需求。使用以下命令將模組安裝到python環境中。

pip install pysftp

示例

在下面的示例中,使用sftp登錄到遠程伺服器,然後獲取在指定目錄中放置一些檔。

import pysftp

with pysftp.Connection('hostname', username='me', password='secret') as sftp:

    with sftp.cd('/allcode'):           # temporarily chdir to allcode
        sftp.put('/pycode/filename')      # upload file to allcode/pycode on remote
        sftp.get('remote_file')         # get a remote file

當運行上面的代碼時,可以看到allcode目錄中存在的檔列表,還可以在指定目錄中放置或獲取一些檔。


上一篇: Python FTP 下一篇: Python Web伺服器