Repository navigation
Open
Description
ENV:
Host:
Mac OS X ver. 10.10.5 BuildVersion: 14F1713
Python 2.7.10
paramiko (1.16.0)
Remote:
CentOS Linux release 7.1.1503 (Core)
Python 2.7.5
Create paramiko client and sftp client and download a remote file:
>>> import paramiko
>>> client = paramiko.SSHClient()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect("192.168.50.3", username="vagrant", password="vagrant", port=22)
>>> sftp = client.open_sftp()
>>> sftp.get(".bashrc", "downloaded/.bashrc")
Checking that file indeed was downloaded (another terminal tab):
$ ls -la downloaded/
total 8
drwxr-xr-x 3 dmitry.tokarev US\Domain Users 102 Apr 18 11:25 .
drwxr-xr-x 29 dmitry.tokarev US\Domain Users 986 Apr 18 11:21 ..
-rw-r--r-- 1 dmitry.tokarev US\Domain Users 231 Apr 18 11:25 .bashrc
Now close sftp and try to get() the file again:
>>> sftp.get(".bashrc", "downloaded/.bashrc_new")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/paramiko/sftp_client.py", line 721, in get
size = self.getfo(remotepath, fl, callback)
File "/Library/Python/2.7/site-packages/paramiko/sftp_client.py", line 689, in getfo
file_size = self.stat(remotepath).st_size
File "/Library/Python/2.7/site-packages/paramiko/sftp_client.py", line 413, in stat
t, msg = self._request(CMD_STAT, path)
File "/Library/Python/2.7/site-packages/paramiko/sftp_client.py", line 729, in _request
num = self._async_request(type(None), t, *arg)
File "/Library/Python/2.7/site-packages/paramiko/sftp_client.py", line 751, in _async_request
self._send_packet(t, msg)
File "/Library/Python/2.7/site-packages/paramiko/sftp.py", line 170, in _send_packet
self._write_all(out)
File "/Library/Python/2.7/site-packages/paramiko/sftp.py", line 133, in _write_all
n = self.sock.send(out)
File "/Library/Python/2.7/site-packages/paramiko/channel.py", line 698, in send
return self._send(s, m)
File "/Library/Python/2.7/site-packages/paramiko/channel.py", line 1058, in _send
raise socket.error('Socket is closed')
socket.error: Socket is closed
>>>
As expected we got Socket is closed error.
Now let's check what happened with local filesystem:
$ ls -la downloaded/
total 8
drwxr-xr-x 4 dmitry.tokarev US\Domain Users 136 Apr 18 11:29 .
drwxr-xr-x 29 dmitry.tokarev US\Domain Users 986 Apr 18 11:21 ..
-rw-r--r-- 1 dmitry.tokarev US\Domain Users 231 Apr 18 11:25 .bashrc
-rw-r--r-- 1 dmitry.tokarev US\Domain Users 0 Apr 18 11:29 .bashrc_new
We got a new empty file .bashrc_new.
I understand that paramiko creates a local file to write to before opening a remote file, but i would expect that if sftp client is closed it would not create the empty file or it would remove it.
I get same results if i close "main" ssh client (client.close()) >> empty file still gets created.
Activity
bitprophet commentedon Apr 25, 2016
Cute. Definitely sounds like an order of operations / failure to clean up, bug. Thanks for the detailed report! I don't have time to dive in right now but adding to a release milestone.
MrMino commentedon Jan 29, 2019
From a fast look on the code,
SFTP.get()does this as its first few instructions:This means, that the file is created first, no matter what happens.
Since using io buffer here won't work (big files will lead to OOM errors), maybe a "lazy file class", that opens the file only after the first write is called?