OSError:尺寸不匹配! 4628344!= 330596

问题描述 投票:-1回答:1

我正在尝试使用SFTP python库pysftp将文件发送到远程邮箱服务器。但是,当我尝试放入文件时,出现此错误:

我正在使用python 3.7.4

Traceback (most recent call last):
 File "SFTP.py", line 54, in <module>
   srv.put('file.zip','file.zip')
 File "C:\Users\HP\Anaconda3\lib\site-packages\pysftp\__init__.py", line 364, in put
   confirm=confirm)
 File "C:\Users\HP\Anaconda3\lib\site-packages\paramiko\sftp_client.py", line 759, in put
   return self.putfo(fl, remotepath, file_size, callback, confirm)
 File "C:\Users\HP\Anaconda3\lib\site-packages\paramiko\sftp_client.py", line 723, in putfo
   "size mismatch in put!  {} != {}".format(s.st_size, size)
OSError: size mismatch in put!  4628344 != 330596

PS:我尝试了另一台远程服务器,它运行良好。这是我的代码

import pysftp
import os
cnopts = pysftp.CnOpts()
hostkeys = None
host="host.com"
username=username
password=password

if cnopts.hostkeys.lookup(host) == None:
    print("New host - will accept any host key")
    # Backup loaded .ssh/known_hosts file
    hostkeys = cnopts.hostkeys
    # And do not verify host key of the new host
    cnopts.hostkeys = None

with pysftp.Connection(host=host, username=username, password=password, cnopts=cnopts) as sftp:        
    if hostkeys != None:
        print("Connected to new host, caching its hostkey")
        hostkeys.add(host, sftp.remote_server_key.get_name(), sftp.remote_server_key)
        hostkeys.save(pysftp.helpers.known_hosts())


srv = pysftp.Connection(host=host, username=username,password=password)
srv.put('file.zip','file.zip')
python pysftp
1个回答
0
投票

我找到了一种解决方法,但不建议这样做,我只通过注释引发异常的代码块就可以解决问题:

 with self.file(remotepath, "wb") as fr:
            fr.set_pipelined(True)
            size = self._transfer_with_callback(
                reader=fl, writer=fr, file_size=file_size, callback=callback
            )
        if confirm:
            s = self.stat(remotepath)
            if s.st_size != size:
                s = SFTPAttributes()
                pass
                # raise IOError(
                #     "size mismatch in put!  {} != {}".format(s.st_size, size)
                # )
        else:
            s = SFTPAttributes()
        return s

文件为site-packages\paramiko\sftp_client.py。但是该解决方案还会导致文件大小出现其他问题。每当我运行put()方法时,服务器中传输的文件就会累积起来,因此您需要注意这一点。

编辑:

设置后检查参数符合False后工作。因此,无需修改paramiko代码。

srv.put('file.zip','file.zip',confirm=False)
© www.soinside.com 2019 - 2024. All rights reserved.