如何使用pysftp/paramiko读取远程SFTP服务器中文件的内容和属性
我正在使用以下代码从两个不同的节点中获取列表中的文件对象列表。 我的目的是获取文件元数据并以后比较文件。但是使用filelist = mysftp.listdir()
,我只获取文件名,并且无法获取诸如file.content.content
或file.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.content
与file2.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要检索文件属性,请使用 而不是
connection.listdir
。要阅读内容,请使用 。
此外,请勿设置
cnopts.hostkeys = none
,除非您不关心安全性。有关正确的解决方案,请参见使用pysftpp 验证主机密钥。
尽管您的更好使用paramiko代替pysftp 。
。 ://stackoverflow.com/q/51298467/850848“>如何通过paramiko 在目录中获取所有SFTP文件的大小。
To retrieve file attributes, use
Connection.listdir_attr
instead ofConnection.listdir
.To read the contents, use
Connection.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.