2828from gevent import sleep , socket , get_hub
2929from gevent .hub import Hub
3030from ssh2 .error_codes import LIBSSH2_ERROR_EAGAIN
31- from ssh2 .exceptions import SFTPHandleError , \
32- SFTPIOError as SFTPIOError_ssh2
31+ from ssh2 .exceptions import SFTPHandleError , SFTPProtocolError
3332from ssh2 .session import Session
3433from ssh2 .sftp import LIBSSH2_FXF_READ , LIBSSH2_FXF_CREAT , LIBSSH2_FXF_WRITE , \
3534 LIBSSH2_FXF_TRUNC , LIBSSH2_SFTP_S_IRUSR , LIBSSH2_SFTP_S_IRGRP , \
@@ -416,18 +415,12 @@ def _make_sftp(self):
416415 sftp = self .session .sftp_init ()
417416 except Exception as ex :
418417 raise SFTPError (ex )
419- errno = self .session .last_errno ()
420- while (sftp is None and errno == LIBSSH2_ERROR_EAGAIN ) \
421- or sftp == LIBSSH2_ERROR_EAGAIN :
418+ while sftp == LIBSSH2_ERROR_EAGAIN :
422419 wait_select (self .session )
423420 try :
424421 sftp = self .session .sftp_init ()
425422 except Exception as ex :
426423 raise SFTPError (ex )
427- errno = self .session .last_errno ()
428- if sftp is None and errno != LIBSSH2_ERROR_EAGAIN :
429- raise SFTPError ("Error initialising SFTP - error code %s" ,
430- errno )
431424 return sftp
432425
433426 def _mkdir (self , sftp , directory ):
@@ -449,7 +442,7 @@ def _mkdir(self, sftp, directory):
449442 LIBSSH2_SFTP_S_IXOTH
450443 try :
451444 self ._eagain (sftp .mkdir , directory , mode )
452- except SFTPIOError_ssh2 as error :
445+ except SFTPProtocolError as error :
453446 msg = "Error occured creating directory %s on host %s - %s"
454447 logger .error (msg , directory , self .host , error )
455448 raise SFTPIOError (msg , directory , self .host , error )
@@ -485,7 +478,7 @@ def copy_file(self, local_file, remote_file, recurse=False,
485478 if destination is not None :
486479 try :
487480 self ._eagain (sftp .stat , destination )
488- except SFTPHandleError :
481+ except ( SFTPHandleError , SFTPProtocolError ) :
489482 self .mkdir (sftp , destination )
490483 self .sftp_put (sftp , local_file , remote_file )
491484 logger .info ("Copied local file %s to remote destination %s:%s" ,
@@ -508,7 +501,7 @@ def sftp_put(self, sftp, local_file, remote_file):
508501 self ._sftp_put (remote_fh , local_file )
509502 # THREAD_POOL.apply(
510503 # sftp_put, args=(self.session, remote_fh, local_file))
511- except SFTPIOError_ssh2 as ex :
504+ except SFTPProtocolError as ex :
512505 msg = "Error writing to remote file %s - %s"
513506 logger .error (msg , remote_file , ex )
514507 raise SFTPIOError (msg , remote_file , ex )
@@ -539,7 +532,7 @@ def mkdir(self, sftp, directory, _parent_path=None):
539532 _dir = '/' .join ((_parent_path , _dir ))
540533 try :
541534 self ._eagain (sftp .stat , _dir )
542- except SFTPHandleError as ex :
535+ except ( SFTPHandleError , SFTPProtocolError ) as ex :
543536 logger .debug ("Stat for %s failed with %s" , _dir , ex )
544537 self ._mkdir (sftp , _dir )
545538 if sub_dirs is not None :
@@ -582,7 +575,7 @@ def copy_remote_file(self, remote_file, local_file, recurse=False,
582575 sftp = self ._make_sftp () if sftp is None else sftp
583576 try :
584577 self ._eagain (sftp .stat , remote_file )
585- except SFTPHandleError :
578+ except ( SFTPHandleError , SFTPProtocolError ) :
586579 msg = "Remote file or directory %s does not exist"
587580 logger .error (msg , remote_file )
588581 raise SFTPIOError (msg , remote_file )
@@ -635,7 +628,7 @@ def scp_recv(self, remote_file, local_file, recurse=False, sftp=None,
635628 if recurse :
636629 try :
637630 self ._eagain (sftp .stat , remote_file )
638- except SFTPHandleError :
631+ except ( SFTPHandleError , SFTPProtocolError ) :
639632 msg = "Remote file or directory %s does not exist"
640633 logger .error (msg , remote_file )
641634 raise SCPError (msg , remote_file )
@@ -744,7 +737,7 @@ def scp_send(self, local_file, remote_file, recurse=False, sftp=None):
744737 sftp = self ._make_sftp () if sftp is None else sftp
745738 try :
746739 self ._eagain (sftp .stat , destination )
747- except SFTPHandleError :
740+ except ( SFTPHandleError , SFTPProtocolError ) :
748741 self .mkdir (sftp , destination )
749742 self ._scp_send (local_file , remote_file )
750743 logger .info ("SCP local file %s to remote destination %s:%s" ,
@@ -807,7 +800,7 @@ def sftp_get(self, sftp, remote_file, local_file):
807800 # cannot be used simultaneously in multiple threads.
808801 # THREAD_POOL.apply(
809802 # sftp_get, args=(self.session, remote_fh, local_file))
810- except SFTPIOError_ssh2 as ex :
803+ except SFTPProtocolError as ex :
811804 msg = "Error reading from remote file %s - %s"
812805 logger .error (msg , remote_file , ex )
813806 raise SFTPIOError (msg , remote_file , ex )
0 commit comments