Connect to SFTP with key file using Python pysftp

╄→尐↘猪︶ㄣ 提交于 2019-12-09 18:24:26

问题


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?

Thank you!

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

回答1:


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")

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.

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



来源:https://stackoverflow.com/questions/53875954/connect-to-sftp-with-key-file-using-python-pysftp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!