Reputation: 173
I was trying to send a file using SFTP python library pysftp
to a remote mailbox server. But when I try to put the file I get this Error:
I'm using 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:I tried with another remote server and it works perfectly. This is my code
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')
Upvotes: 3
Views: 8054
Reputation: 173
I found a workaround solution but not recommended, I was able to go through the problem just by commenting the bloc of code where the exception is raised:
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
the file is site-packages\paramiko\sftp_client.py
. but the solution causes other problems with file size. the file transferred in the server gets cumulated whenever I run the put() method so you need to be careful with this.
EDIT:
worked after setting the post-check parameter conform to False. So no need to modify paramiko
code.
srv.put('file.zip','file.zip',confirm=False)
Upvotes: 6