SFTP被称为SSH文件传输协议 ,也被称为安全文件传输协议。 SFTP是一个网络协议,通过任何可靠的数据流提供文件访问、传输和文件管理。
Python sftp
Python pysftp模块是SFTP的一个简单接口。它提供了高层次的抽象和基于任务的例程来处理SFTP的需求。然而,SFTP协议不支持认证和安全;它期望底层协议能保证其安全。因此, SFTP被最广泛地用作SSH 协议第二版的实现子系统,由同一个工作组设计。
让我们来安装 sftp模块pysftp。
安装pysftp
pip install pysftp
# or
python3 -m pip install pysftp
取决于你的Python和pip版本,它将安装在你的系统中。
如果你不知道如何升级pip,请查看升级pip指南。
如何使用PySftp访问SFTP服务器
你可以在Python中使用pysftp列出目录的内容。为此,你需要你的主机名、用户名和密码。
然后你需要使用cwd或chdir方法从目录中切换,并提供远程目录作为第一个参数:
import pysftp
myHostname = "newblog.com"
myUsername = "root"
myPassword = "root"
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:
print("Connection succesfully stablished ... ")
# Switch to a remote directory
sftp.cwd('/var/www/vhosts/')
# Obtain structure of the remote directory '/var/www/vhosts'
directory_structure = sftp.listdir_attr()
# Print data
for attr in directory_structure:
print(attr.filename, attr)
# connection closed automatically at the end of the with statement
当然,这只是一个不存在的假服务器。尽管如此,在现实生活中,为了安全起见,你必须使用环境变量来获取任何文件中的实际凭证,不要把所有的凭证放在单个文件中。总是放在环境变量文件里面。例如,.env 文件。
现在,让我们看看正在发生什么。上面的代码对你来说也是一样的,你要输入你的凭证,代码就会为你工作。
首先,我们导入了pysftp模块,然后提供了我的主机名、我的用户名和我的密码 。
然后我们使用Python with 语句,通过提供主机名、用户名和密码来打开与远程服务器的安全连接。如果成功的话,我们将切换远程目录来获取列表并在控制台中逐一打印。
该列表的顺序是任意的。它不包括唯一条目'.'和'.'。返回的SFTPAttributes对象将各自有一个额外的字段:longname,它可能包含UNIX格式的文件属性的格式化字符串。该字符串的内容将取决于SFTP服务器。
如何在 Python 中使用 pysftp 上传文件
要通过SFTP使用pysftp上传文件到远程服务器,你需要使用SFTP客户端的sftp.put()方法。put方法的第一个参数是你要上传的文件的相对或绝对本地路径,第二个参数是该文件应该被上传的远程路径:
import pysftp
myHostname = "newblog.com"
myUsername = "root"
myPassword = "root"
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:
print("Connection succesfully stablished ... ")
# Define a file that you want to upload from your local directorty
# or absolute "/Users/krunal/Desktop/code/pyt/app.txt"
localFilePath = './app.txt'
# Define the remote path where the file will be uploaded
remoteFilePath = '/var/backups/app.txt'
# Use put method to upload a file
sftp.put(localFilePath, remoteFilePath)
# connection closed automatically at the end of the with statement
我们首先在这段代码中建立了一个安全连接,然后定义了两个文件路径:
- localFilePath:它是本地文件的路径
- remoteFilePath:它是通往远程文件的路径
然后我们使用了sftp.put()方法将文件上传到服务器。
如何使用pysftp下载远程文件
在上一节中,我们已经看到了如何上传文件。现在让我们看看如何下载一个文件。
要使用pysftp从服务器上下载一个远程文件,我们必须打开一个连接,并从sftp实例中使用get方法,该方法希望得到将被下载的远程文件的路径,而第二个参数是文件应该被存储的本地路径:
import pysftp
myHostname = "newblog.com"
myUsername = "root"
myPassword = "root"
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:
print("Connection succesfully stablished ... ")
# Define the remote path file path
remoteFilePath = '/var/backups/app.txt'
# Define a directory in which you have to save the file.
# or absolute "/Users/krunal/Desktop/code/pyt/app.txt"
localFilePath = './app.txt'
# Use get method to download a file
sftp.get(remoteFilePath, localFilePath)
# connection closed automatically at the end of the with statement
我们在上面的代码中定义了一个连接,然后定义了两个文件路径:
- remoteFilePath:这是一个文件所在的路径
- localFilePath:它是一个将被下载的文件的路径
在接下来的步骤中,我们使用get()方法来下载文件。
如何在 Python 中使用 pysftp 删除一个文件
你可以使用 sftp.remove() 方法用 pysftp 删除一个文件。remove()方法希望将远程文件的绝对路径作为第一个参数:
import pysftp
myHostname = "newblog.com"
myUsername = "root"
myPassword = "root"
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:
print("Connection succesfully stablished ... ")
# Define the remote path file path
removeFilePath = '/var/backups/app.txt'
sftp.remove(removeFilePath)
# connection closed automatically at the end of the with statement
在这段代码中,我们打开了一个连接,然后定义了一个removeFilePath变量,它包含需要删除的文件的路径。
然后我们使用sftp.remove()方法从远程服务器上删除一个文件。
PySftp模块有很多方法,你可以用它们来做多种事情,比如处理权限等,所以别忘了看看这里的官方文档网站。
本教程就到此为止。