用ssh用ssh从服务器读取一个文件 [英] Read a file from server with ssh using python

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

问题描述

我试图从服务器上使用ssh从python读取文件。我正在使用paramiko来连接。我可以连接到服务器,并运行一个命令,如猫的文件名,并从服务器获取数据,但一些文件,我想读的大小约1 GB或更多。



如何在服务器上使用python逐行读取文件?



附加信息:定期执行的操作是运行'cat filename'命令并将结果存储在一个变量中,并解决这个问题。但由于这里的文件是相当大的,我正在寻找一种方式来逐行读取文件服务器。



编辑:我可以读一堆数据并将其拆分成行,但问题是缓冲区中收到的数据并不总是包含完整的行。例如,如果缓冲区有300行,最后一行可能只是服务器上的一半行,下一半将在下次调用服务器时被取出。我想要完整的行



编辑2:我可以使用什么命令来在一定范围内的文件中打印行。像打印第100行,然后下100等?这种方式缓冲区将始终包含完整的行。

解决方案

SFTPClient 类允许你得到一个类似文件的对象,以Pythonic的方式从远程文件中读取数据。 / p>

假设你有一个开放的 SSHClient



<$ p $

$ remote_file = sftp_client.open('remote_filename')
尝试:
for remote_file中的行:
#进程
最后:
remote_file.close()


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.

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

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.

EDIT: 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

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's SFTPClient class allows you to get a file-like object to read data from a remote file in a Pythonic way.

Assuming you have an open 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()

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

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