使用Python pysftp使用密钥文件连接到SFTP

rub*_*119 5 python sftp file pysftp

我需要连接到SFTP,下载最新文件,然后更改文件名并再次加载到相同的SFTP文件夹并删除“原始名称”文件。我已经使用用户名和密码通过FTP完成了此操作,但是在这种情况下,SFTP具有一个密钥文件(.ppk)。如何将密钥文件设置为密码?

谢谢!

import pysftp

srv = pysftp.Connection(host="your_FTP_server", username="your_username",
password="your_password")

# Get the directory and file listing
data = srv.listdir()

# Closes the connection
srv.close()

# Prints out the directories and files, line by line
for i in data:
   print i
Run Code Online (Sandbox Code Playgroud)

b13*_*3rg 5

要使用密钥文件进行连接,您将需要在创建连接时将路径传递给密钥文件。为此,将参数“ private_key”设置为文件路径。

您上面的代码应如下所示:

srv = pysftp.Connection(host="you_FTP_server", username="your_username", private_key="./Path/To/File")
Run Code Online (Sandbox Code Playgroud)

pySFTP启动连接时,它将尝试使用您传入的文件。如果由于密钥文件而失败,它将引发身份验证异常。

这是我找到答案的链接:https : //pysftp.readthedocs.io/en/release_0.2.7/pysftp.html