Unable to abort a FTP command?

Scott David Daniels Scott.Daniels at Acm.Org
Wed Apr 15 14:34:13 EDT 2009


_wdx at 163.com wrote:
> Thank you. You are right, retrbinary did not notice I want to abort,
> so it won't break the recv loop and close data connection....
>     def getpart(self, ftp_filename, rest, cnt, out_filename):
>         self.outf = open(out_filename, 'wb')
>         self.cnt = cnt
>         self.handle.voidcmd('TYPE I')
>         conn = self.handle.transfercmd('RETR ' + ftp_filename, rest)
>         while 1:
>             data = conn.recv(8192)
>             if not data:
>                 break
>             if self.getpart_callback(data):
>                 try:
>                     self.handle.abort()
>                     break
>                 except:
>                     pass
>         self.outf.close()
>         self.handle.voidresp()
>         conn.close()
> 
How about:
      import logging
      ...

      def getpart(self, ftp_filename, rest, cnt, out_filename):
          with open(out_filename, 'wb') as outf:
              self.outf = outf
              self.cnt = cnt
              self.handle.voidcmd('TYPE I')
              conn = self.handle.transfercmd('RETR ' + ftp_filename,
                                             rest)
              data = conn.recv(8192)
              while data:
                  if self.getpart_callback(data):
                      try:
                          self.handle.abort()
                          break
                      except BaseException, why:
                          # "except BaseException as why:" if >= Py2.6
                          logging.error('%s: %s encountered' % (
                                        (why.__class__.__name__, why))
                          if not isinstance(why, Exception):
                              raise
                  data = conn.recv(8192)
          self.handle.voidresp()
          conn.close()

--Scott David Daniels
Scott.Daniels at Acm.Org




More information about the Python-list mailing list