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.
This commit is contained in:
parent
48afc3082a
commit
a9c51b23ce
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue