如果我使用PYSFTP传输文件,如何验证成功上传?
我正在使用PYSFTP通过网络传输文件。我想确保在此过程中的可靠性。 PYSFTP中有一个 put
API,它返回 sftpattributes
包含传输文件大小的对象。这足以验证成功的转移,我的意思是通过比较大小。
或...在我看来,我也可以打开传输的文件并使用检查
方法...是正确的方法吗?我有点困惑。谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
已经检查上传文件的大小。这就是
vircent = true
参数的目的,您无需做更多的事情:而理论上可以使用
sftpfile.check.check
验证校验和,它通常不起作用,因为大多数SFTP服务器,包括广泛的SFTP服务器OpenSSH,不支持计算校验和。因此电话将失败。您必须求助于运行一些shell命令来计算校验和检查。请参阅:将下载文件的MD5与Python中的SFTP服务器上的文件进行比较
,但值得怀疑的是,是否值得努力,请参阅:<<<<<<<<<< br>
如何在SFTP文件传输过程中执行charchsums的数据完整性?
,尽管这些天,您不应使用Pysftp,您不应使用Pysftp,因为它已经死了。直接使用Paramiko。参见 pysftp vs. paramiko 。请参阅基本上有关paramiko的问题:如何检查paramiko是否成功将文件上传到SFTP服务器?
Pysftp
Connection.put
already checks the size of the uploaded file. That's what theconfirm=True
parameter is for, You do not need to do anything more:While you can theoretically verify the checksum with
SFTPFile.check
, it won't typically work, as most SFTP servers, including the widespread OpenSSH, do not support calculating checksums. So the call will fail. You would have to resort to running some shell command to calculate the checksum. See:Comparing MD5 of downloaded files against files on an SFTP server in Python
But it's questionable whether it is worth the effort, see:
How to perform checksums during a SFTP file transfer for data integrity?
Though these days, you should not use pysftp, as it is dead. Use Paramiko directly instead. See pysftp vs. Paramiko. See basically the same question about Paramiko: How to check if Paramiko successfully uploaded a file to an SFTP server?
在检查文件大小已经是成功传输的一个很好的指标时,它不会告诉您是否有些翻转,因此远程文件没有完全相同的内容。因此,您应该使用校验和,例如
md5
,请参见 https://en.wikipedia.org/wiki/md5 。基本上,这就是
检查
方法(https://docs.paramiko.org/en/latest/api/sftp.html?highlight=paramiko.sftp_file.sftpfile#paramiko.sftp_file.SFTPFile.check)您提到的将计算。然后,您必须为本地文件计算相同的校验和比较。While checking the file size is already a good indicator for a successful transfer, it will not tell you if a bit has flipped and therefore the remote file does not have exactly the same content. Therefore, you should use a checksum, e.g. provided by a hashing algorithm like
md5
, see https://en.wikipedia.org/wiki/MD5.Basically this is what the
check
method (https://docs.paramiko.org/en/latest/api/sftp.html?highlight=paramiko.sftp_file.sftpfile#paramiko.sftp_file.SFTPFile.check) you mentioned will compute. Then you must compute the same checksum for your local file and compare.