从Python Paramiko中的SFTP服务器下载文件

发布于 2025-02-06 08:03:21 字数 85 浏览 2 评论 0 原文

我正在使用Python Paramiko从SFTP服务器检索/搜索文件。我在目录中获取所有文件。

我需要的是该目录中的特定文件。我该如何明白?

I am using Python Paramiko to retrieve/search file from an SFTP server. I get all file in the directory.

What I need is specific file from that directory. How do I get it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆躺在深渊里 2025-02-13 08:03:22

使用paramiko 要下载一个文件:

with paramiko.SSHClient() as ssh:
    ssh.connect(host, username=username, password=password)
    with ssh.open_sftp() as sftp:
        sftp.get("/remote/path/file.txt", "/local/path/file.txt")

您还必须处理服务器的主机密钥验证

Use Paramiko SFTPClient.get to download a single file:

with paramiko.SSHClient() as ssh:
    ssh.connect(host, username=username, password=password)
    with ssh.open_sftp() as sftp:
        sftp.get("/remote/path/file.txt", "/local/path/file.txt")

You will also have to deal with the server's host key verification.

夏天碎花小短裙 2025-02-13 08:03:22

这是一个答案,以防您需要使用SFTP连接的一种查找,而不知道文件的确切路径和名称。如果不是您想要的,我很抱歉。

我制作了一个名为 paramiko ,它实现了高级功能,例如 glob 。要找到一个特定的文件并下载它,您可以这样做:

from sftputil import SFTP

sftp = SFTP("hostname", "username", password="password")

# Here we look for a file with a name starting with `foo`
found_files = sftp.glob("directory/foo*")

# Here we look for the file `bar` in any subdirectory
found_files = sftp.glob("directory/*/bar")

# But you can use other patterns of course.

# And now the files can be downloaded
for f in found_files:
    sftp.get(f, "my/local/path")

如果您不知道 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 :

from sftputil import SFTP

sftp = SFTP("hostname", "username", password="password")

# Here we look for a file with a name starting with `foo`
found_files = sftp.glob("directory/foo*")

# Here we look for the file `bar` in any subdirectory
found_files = sftp.glob("directory/*/bar")

# But you can use other patterns of course.

# And now the files can be downloaded
for f in found_files:
    sftp.get(f, "my/local/path")

If you don’t know glob, you should read the python documentation, as this implementation works the same way.

雾里花 2025-02-13 08:03:22

您需要做的是创建一个SSH客户端,然后用管道 GREP 执行 ls 以查找您的文件。例如 ls /srv /ftp | grep'^ftp _'/srv/ftp 目录中查找文件,并以 ftp 开始。然后打开一个SFTP连接并执行 get 命令以将文件放置。

编辑:下面的马丁提到,有一种更好的方法可以使用sftpclient.listdir()获取目录内容 - 我已经修改了该方法。文档中的更多信息: https://docs.paramiko.orgg/en/稳定/api/sftp.html

将所有这些放在一起看来

import paramiko

host = ''
port = 22
username = ''
password = ''

with paramiko.SSHClient() as client:
    client.connect(host, port, username, password)  
    with client.open_sftp() as sftp:
        files = sftp.listdir('/srv/ftp')
        for i, file in enumerate(files):
            if file and file.startswith('FTP'):
                sftp.get(f'/srv/ftp/{file}', f'~/ftp/{file}')
                print(f'Moved {file}')

该代码未经测试,但应该起作用。希望这很清楚。

What you need to do is create an ssh client, then execute an ls with piped grep to find your file. such as ls /srv/ftp | grep '^FTP_' to find files in the /srv/ftp directory and start with FTP. Then open an sftp connection and execute the get 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

import paramiko

host = ''
port = 22
username = ''
password = ''

with paramiko.SSHClient() as client:
    client.connect(host, port, username, password)  
    with client.open_sftp() as sftp:
        files = sftp.listdir('/srv/ftp')
        for i, file in enumerate(files):
            if file and file.startswith('FTP'):
                sftp.get(f'/srv/ftp/{file}', f'~/ftp/{file}')
                print(f'Moved {file}')

This code is untested but should work. Hope that's clear.

神爱温柔 2025-02-13 08:03:22

使用paramiko sftpclient.get下载单个文件:

第一个安装: pip install paramiko 来自

import paramiko
with paramiko.SSHClient() as ssh:
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, username=username, password=password)
    with ssh.open_sftp() as sftp:
        sftp.get("/remote/path/file.txt", "/local/path/file.txt")

注意: 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.

import paramiko
with paramiko.SSHClient() as ssh:
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, username=username, password=password)
    with ssh.open_sftp() as sftp:
        sftp.get("/remote/path/file.txt", "/local/path/file.txt")

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) use AutoAddPolicy ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文