From a9c51b23cea3a912cf675e65b867e297a39ce9aa Mon Sep 17 00:00:00 2001 From: Robey Pointer Date: Sun, 12 Nov 2006 20:17:42 -0800 Subject: [PATCH] [project @ robey@lag.net-20061113041742-e24468a63d31b8bd] sometimes the sftp module is used with raw sockets, not just paramiko Channels. in this case, calling recv() will never return. so notice this and use select() to give python a chance to notice a closed socket. this kind of thing is especially useful for unit tests. --- paramiko/sftp.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/paramiko/sftp.py b/paramiko/sftp.py index 8d1db46..0b2bbe2 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -16,6 +16,7 @@ # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. +import select import socket import struct @@ -147,7 +148,20 @@ class BaseSFTP (object): def _read_all(self, n): out = '' while n > 0: - x = self.sock.recv(n) + if isinstance(self.sock, socket.socket): + # sometimes sftp is used directly over a socket instead of + # through a paramiko channel. in this case, check periodically + # if the socket is closed. (for some reason, recv() won't ever + # return or raise an exception, but calling select on a closed + # socket will.) + while True: + read, write, err = select.select([ self.sock ], [], [], 0.1) + if len(read) > 0: + x = self.sock.recv(n) + break + else: + x = self.sock.recv(n) + if len(x) == 0: raise EOFError() out += x