使用 Python 使用 SSH 从服务器读取文件 [英] Read a file from server with SSH using Python

查看:39
本文介绍了使用 Python 使用 SSH 从服务器读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 中的 SSH 从服务器读取文件.我正在使用 Paramiko 进行连接.我可以连接到服务器并运行类似 cat filename 的命令并从服务器取回数据,但我尝试读取的某些文件大小约为 1 GB 或更大.

I am trying to read a file from a server using SSH from Python. I am using Paramiko to connect. I can connect to the server and run a command like cat filename and get the data back from the server but some files I am trying to read are around 1 GB or more in size.

如何使用 Python 逐行读取服务器上的文件?

How can I read the file on the server line by line using Python?

附加信息:通常执行的是运行 cat filename 命令并将结果存储在变量中并对其进行处理.但是由于这里的文件很大,我正在寻找一种从服务器上逐行读取文件的方法.

Additional Info: What is regularly do is run a cat filename command and store the result in a variable and work off that. But since the file here is quite big, I am looking for a way to read a file line by line off the server.

我可以读取一堆数据并将其拆分为行,但问题是缓冲区中接收到的数据并不总是包含完整的行.例如,如果缓冲区有 300 行,则最后一行可能只是服务器上该行的一半,而下一半将在下一次调用服务器时获取.我想要完整的线条

I can read a bunch of data and split it into lines but the problem is that the data received in the buffer does not always include the complete lines. For eg, if buffer has 300 lines, the last line may only be half of the line on the server and the next half would be fetched in the next call to the server. I want complete lines

编辑 2:我可以使用什么命令在特定范围内打印文件中的行.比如打印前 100 行,然后打印下 100 行等等?这样缓冲区将始终包含完整的行.

EDIT 2: what command can I use to print lines in a file in a certain range. Like print first 100 lines, then the next 100 and so on? This way the buffer will always contain complete lines.

推荐答案

Paramiko 的 SFTPClient 允许你获取一个类似文件的对象,以 Pythonic 的方式从远程文件中读取数据.

Paramiko's SFTPClient class allows you to get a file-like object to read data from a remote file in a Pythonic way.

假设你有一个开放的SSHClient:

sftp_client = ssh_client.open_sftp()
remote_file = sftp_client.open('remote_filename')
try:
    for line in remote_file:
        # process line
finally:
    remote_file.close()

这篇关于使用 Python 使用 SSH 从服务器读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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