diff options
author | Robey Pointer <robey@lag.net> | 2006-11-12 20:17:42 -0800 |
---|---|---|
committer | Robey Pointer <robey@lag.net> | 2006-11-12 20:17:42 -0800 |
commit | a9c51b23cea3a912cf675e65b867e297a39ce9aa (patch) | |
tree | 8337e397164fab167247a15728cd48cea84f8088 | |
parent | 48afc3082a841ca5416fbf7ba8db94f190221638 (diff) |
[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.
-rw-r--r-- | paramiko/sftp.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/paramiko/sftp.py b/paramiko/sftp.py index 8d1db468..0b2bbe29 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 |