我需要连接到 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
原文由 rubio119 发布,翻译遵循 CC BY-SA 4.0 许可协议
要使用密钥文件进行连接,您需要在创建连接时传递密钥文件的路径。为此,您需要将参数“private_key”设置为文件路径。
你上面的代码应该是这个样子:
当pySFTP发起连接时,它会尝试使用你传入的文件。如果因为keyfile而失败,它会抛出认证异常。
这是我找到答案的链接: https ://pysftp.readthedocs.io/en/release_0.2.7/pysftp.html。