首页 > 编程知识 正文

Python pysftp模块

时间:2023-11-26 13:13:11 阅读:309361 作者:QYGE

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

说明:

在上面的代码片段中,我们已经建立了安全连接,然后定义了两个文件路径- 本地 文件 路径和远程 文件 路径。

  1. local_File_Path: 此文件路径是指向本地文件的路径。
  2. 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

说明:

在上面的代码片段中,我们定义了一个连接,然后定义了两个文件路径- 远程 文件 路径和本地 文件 路径

  1. remote_File_Path: 该文件路径是文件所在的路径。
  2. 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 模块的官方文档。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。