如何在Python中使用PySftp访问SFTP服务器

本文概述

  • 1.安装PySftp
  • 2.用法
使用你自己的脚本通过SFTP处理事物总是有帮助的, 如果你使用的是Python, 则PySftp是使用该技术而又不会让人头疼的库, 因为它非常易于使用。 pysftp是Paramiko的包装, 具有更多类似Python的界面。 Paramiko库是一个很棒的python库, 它是pysftp的中坚力量。 pysftp创建的方法是抽象方法, 通过封装许多与SFTP交互的高级功能用例, 从而提高了程序员的工作效率。 pysftp可以使用pysftp提供的功能, 而不是编写自己的代码来遍历目录并调用get和put方法, 不仅要处理paramiko还要处理Python自己的os和stat模块并编写测试(网络上的许多代码段不完整并且不考虑边缘情况)一个完整的库, 用于处理这三个问题。让你专注于主要任务。
1.安装PySftppysftp接口没有公开Paramiko的所有功能, 但是在单个方法中抽象了很多功能。另一方面, pysftp在Paramiko之上实现了更高级的功能, 尤其是递归文件传输。
要使用Pip在你的环境中安装pysftp, 请运行以下命令:
python -m pip install pysftp

【如何在Python中使用PySftp访问SFTP服务器】有关PySftp的更多信息, 请不要忘记访问官方文档网站或pyp存储库。
2.用法该库的用法以及你需要使用该库完成的多个任务将基本上通过许多示例显示:
列出目录中的文件
你可以使用以下代码段列出目录的内容。打开连接后, 你需要使用cwd或chdir方法从目录切换, 并提供远程目录作为第一个参数:
import pysftpmyHostname = "yourserverdomainorip.com"myUsername = "root"myPassword = "12345"with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:print "Connection succesfully stablished ... "# Switch to a remote directorysftp.cwd('/var/www/vhosts/')# Obtain structure of the remote directory '/var/www/vhosts'directory_structure = sftp.listdir_attr()# Print datafor attr in directory_structure:print attr.filename, attr# connection closed automatically at the end of the with-block

这将在控制台中打印一行, 其中包含远程目录中每个文件/目录的SFTPAttributes对象。该列表是任意顺序的。它不包括特殊条目” 。” 和” ..” 。返回的SFTPAttributes对象将各自具有一个附加字段:longname, 它可以包含文件属性的格式化字符串(采用unix格式)。此字符串的内容取决于SFTP服务器:
.skel drwxr-xr-x1 004096 10 Jul 17:39 .skelbestfreehtmlcsstemplates.com drwx--x---1 1000210024096 22 Jul 15:51 bestfreehtmlcsstemplates.comchroot drwxr-x---1 004096 10 Jul 17:39 chrootdefault drwxr-xr-x1 004096 10 Jul 17:36 defaulteagle148.startdedicated.com drwx--x---1 1000010024096 25 Jul 17:53 eagle148.startdedicated.comfs drwxr-xr-x1 004096 10 Jul 18:03 fsfs-passwd drwxr-x---1 999334096 10 Jul 18:03 fs-passwdourcodeworld.com drwx--x---1 1000110024096 10 Sep 15:30 ourcodeworld.comsystem drwxr-xr-x1 004096 19 Jul 10:26 system

下载远程文件
为了下载远程文件, 请打开一个连接, 并从sftp实例使用get方法, 该方法期望将要下载的远程文件的路径, 并作为第二个参数, 应将文件存储在本地路径中:
import pysftpmyHostname = "yourserverdomainorip.com"myUsername = "root"myPassword = "12345"with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:print "Connection succesfully stablished ... "# Define the file that you want to download from the remote directoryremoteFilePath = '/var/integraweb-db-backups/TUTORIAL.txt'# Define the local path where the file will be saved# or absolute "C:\Users\sdkca\Desktop\TUTORIAL.txt"localFilePath = './TUTORIAL.txt'sftp.get(remoteFilePath, localFilePath) # connection closed automatically at the end of the with-block

上传文件
为了通过SFTP将文件上传到你的服务器, 只需使用SFTP客户端的put方法。此方法将要上传的文件的相对或绝对本地路径作为第一个参数, 将文件应上传到的远程路径作为第二个参数:
import pysftpmyHostname = "yourserverdomainorip.com"myUsername = "root"myPassword = "12345"with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:print "Connection succesfully stablished ... "# Define the file that you want to upload from your local directorty# or absolute "C:\Users\sdkca\Desktop\TUTORIAL2.txt"localFilePath = './TUTORIAL2.txt'# Define the remote path where the file will be uploadedremoteFilePath = '/var/integraweb-db-backups/TUTORIAL2.txt'sftp.put(localFilePath, remoteFilePath) # connection closed automatically at the end of the with-block

删除文件
如果你愿意从服务器中删除文件, 则可以使用remove方法来实现, 该方法将远程文件的绝对路径作为第一个参数:
import pysftpmyHostname = "yourserverdomainorip.com"myUsername = "root"myPassword = "12345"with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:print "Connection succesfully stablished ... "# Define the file that you want to upload from your local directortysftp.remove('/var/custom-folder/TUTORIAL2.txt') # connection closed automatically at the end of the with-block

请记住, PySftp具有多种方法, 可用于执行多项操作, 例如处理权限等。因此, 请不要忘记在此处签出官方文档网站。
编码愉快!

    推荐阅读