使用Python pysftp使用密钥文件连接到SFTP [英] Connect to SFTP with key file using Python pysftp

查看:809
本文介绍了使用Python pysftp使用密钥文件连接到SFTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to connect to a SFTP, download the most recent file, then change the file name and load again to the same SFTP folder and delete the 'original name' file. I have done this with FTP with user and password, however in this case, the SFTP has a key file (.ppk). How can set the key file as password?

谢谢!

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

推荐答案

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

To connect using a key file, you will want to pass the path to the key file when creating the connection. To do this, you'll set the parameter "private_key" to the path to the file.

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

Your code above should look something like this:

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

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

When pySFTP initiates the connection, it will try to use the file you passed in. If it fails because of the keyfile, it will throw an authentication exception.

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

Here's the link to where I found the answer: https://pysftp.readthedocs.io/en/release_0.2.7/pysftp.html.

这篇关于使用Python pysftp使用密钥文件连接到SFTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆