从Python Paramiko中的SFTP服务器下载文件
我正在使用Python Paramiko从SFTP服务器检索/搜索文件。我在目录中获取所有文件。
我需要的是该目录中的特定文件。我该如何明白?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在使用Python Paramiko从SFTP服务器检索/搜索文件。我在目录中获取所有文件。
我需要的是该目录中的特定文件。我该如何明白?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
使用paramiko 要下载一个文件:
您还必须处理服务器的主机密钥验证 。
Use Paramiko
SFTPClient.get
to download a single file:You will also have to deal with the server's host key verification.
这是一个答案,以防您需要使用SFTP连接的一种
查找
,而不知道文件的确切路径和名称。如果不是您想要的,我很抱歉。我制作了一个名为 paramiko ,它实现了高级功能,例如
glob
。要找到一个特定的文件并下载它,您可以这样做:如果您不知道
glob
,则应阅读 python文档,因为此实现以相同的方式工作。Here is an answer in case you need a kind of
find
using a SFTP connection, not knowing the exact path and name of the file. If it is not what you were looking for, I am sorry.I made a library named sftputil, based on paramiko, which implements advanced functionalities such as
glob
. To find a specific file and download it, you can do it this way :If you don’t know
glob
, you should read the python documentation, as this implementation works the same way.您需要做的是创建一个SSH客户端,然后用管道 GREP 执行
ls
以查找您的文件。例如ls /srv /ftp | grep'^ftp _'
在/srv/ftp
目录中查找文件,并以ftp
开始。然后打开一个SFTP连接并执行get
命令以将文件放置。编辑:下面的马丁提到,有一种更好的方法可以使用sftpclient.listdir()获取目录内容 - 我已经修改了该方法。文档中的更多信息: https://docs.paramiko.orgg/en/稳定/api/sftp.html
将所有这些放在一起看来
该代码未经测试,但应该起作用。希望这很清楚。
What you need to do is create an ssh client, then execute an
ls
with pipedgrep
to find your file. such asls /srv/ftp | grep '^FTP_'
to find files in the/srv/ftp
directory and start withFTP
. Then open an sftp connection and execute theget
command to bring the files over.EDIT: Martin below mentioned that there is a better way to get the directory contents using SFTPClient.listdir() - I have revised to that method. More info in the docs: https://docs.paramiko.org/en/stable/api/sftp.html
Putting all that together looks like
This code is untested but should work. Hope that's clear.
使用paramiko sftpclient.get下载单个文件:
第一个安装:
pip install paramiko
来自。注意:
SSH.SEN.MISSING_HOST_KEY_POLICY(PARAMIKO.AUTOADDPOLICY())
对于添加SSH键很重要(以防万一,如果安全性不重要(例如开发模式)(例如开发模式)使用autoaddpolicy
)。Use Paramiko SFTPClient.get to download a single file:
First install :
pip install paramiko
from this link.NOTE:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
is important for add ssh key (just in case if security is not important( like develop mode) useAutoAddPolicy
).