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

新手上路,请多包涵

我需要连接到 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 许可协议

阅读 1.1k
1 个回答

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

你上面的代码应该是这个样子:

 srv = pysftp.Connection(host="you_FTP_server", username="your_username", private_key="./Path/To/File")

当pySFTP发起连接时,它会尝试使用你传入的文件。如果因为keyfile而失败,它会抛出认证异常。

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

原文由 b13rg 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题