如何使用pysftp/paramiko读取远程SFTP服务器中文件的内容和属性

发布于 2025-02-09 17:33:52 字数 1411 浏览 3 评论 0原文

我正在使用以下代码从两个不同的节点中获取列表中的文件对象列表。 我的目的是获取文件元数据并以后比较文件。但是使用filelist = mysftp.listdir(),我只获取文件名,并且无法获取诸如file.content.contentfile.size之类的东西。 。

为了测试目的,我仅使用一个EC2节点并将其称为并行。

import pysftp
import itertools 
from multiprocessing import Process, Pool

# Connect/ssh to an instance

file_list = []

def readFileNode(host):
    try:
        cnopts = pysftp.CnOpts()
        cnopts.hostkeys = None

        mysftp = pysftp.Connection(
            host="3.93.XX.XX",
            username="ubuntu",
            private_key="/Users/test.pem",
            cnopts=cnopts,
        )

        mysftp.cwd("/home/ubuntu/demo")
        filelist = mysftp.listdir()

    except Exception:
        print(e)

    return filelist

if __name__ == '__main__':
    p = Pool(2)
    result = p.map(readFileNode, ['3.93.XX.XX', '3.93.XX.XX'])
    print(result)  

    print(result[0])
    print(result[1])
    fileList1=[]
    fileList2=[]
    fileList1=list[0]
    fileList2=list[1]
    
    for (a, b) in zip(fileList1,fileList2):
       print (a, b)
       #compare two files and also get the file metadata

我在result [0]结果[1]中都有两个节点的文件。 但是,我想获取文件对象,以便我可以比较它们并列出属性。即类似file.size之类的东西,将file1.contentfile2.cont2.content进行比较。其中file1来自node1和file2来自node2。

I am using below code to get list of file objects in list from two different nodes.
My purpose is to get the file metadata and compare the files later. But using filelist = mysftp.listdir(), I am only getting the filenames and unable to fetch something like file.content or file.size.

For testing purpose I am using only one EC2 node and calling it parallel.

import pysftp
import itertools 
from multiprocessing import Process, Pool

# Connect/ssh to an instance

file_list = []

def readFileNode(host):
    try:
        cnopts = pysftp.CnOpts()
        cnopts.hostkeys = None

        mysftp = pysftp.Connection(
            host="3.93.XX.XX",
            username="ubuntu",
            private_key="/Users/test.pem",
            cnopts=cnopts,
        )

        mysftp.cwd("/home/ubuntu/demo")
        filelist = mysftp.listdir()

    except Exception:
        print(e)

    return filelist

if __name__ == '__main__':
    p = Pool(2)
    result = p.map(readFileNode, ['3.93.XX.XX', '3.93.XX.XX'])
    print(result)  

    print(result[0])
    print(result[1])
    fileList1=[]
    fileList2=[]
    fileList1=list[0]
    fileList2=list[1]
    
    for (a, b) in zip(fileList1,fileList2):
       print (a, b)
       #compare two files and also get the file metadata

I have files from both the nodes in result[0] and result[1].
But, I want to get the file objects so that I can compare them and list down the attributes. i.e. something like file.size, compare file1.content with file2.content. Where file1 is from node1 and file2 is from node2.

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

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

发布评论

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

评论(1

药祭#氼 2025-02-16 17:33:52

要检索文件属性,请使用 而不是connection.listdir

要阅读内容,请使用

for i in mysftp.listdir_attr(path):
    print i.filename + " " + i.st_mtime + " " + i.st_size
    with mysftp.open(path, 'r', 32768) as f:
        # Here you can use `f` as if it were a local file opened with `os.open`

此外,请勿设置cnopts.hostkeys = none,除非您不关心安全性。
有关正确的解决方案,请参见使用pysftpp
验证主机密钥。


尽管您的更好使用paramiko代替pysftp

。 ://stackoverflow.com/q/51298467/850848“>如何通过paramiko 在目录中获取所有SFTP文件的大小。

To retrieve file attributes, use Connection.listdir_attr instead of Connection.listdir.

To read the contents, use Connection.open.

for i in mysftp.listdir_attr(path):
    print i.filename + " " + i.st_mtime + " " + i.st_size
    with mysftp.open(path, 'r', 32768) as f:
        # Here you can use `f` as if it were a local file opened with `os.open`

Additionally, do not set the cnopts.hostkeys = None, unless you do not care about security.
For the correct solution, see Verify host key with pysftp
.


Though your better use Paramiko instead of pysftp.

See How to fetch sizes of all SFTP files in a directory through Paramiko.

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