title: How to stop ftp from downloading in python?
body: <p>Here are some bits of code I use to download through ftp. I was trying to stop the download   then continue or redownload it afterwards. I've tried ftp.abort() but it only hangs and returns timeout.</p>
  
  <pre><code>ftplib.error_proto: 421 Data timeout. Reconnect. Sorry.
  </code></pre>
  
  <p><strong>SCENARIO:</strong> 
  The scenario is the user will choose the file to download, while downloading, the user can stop the current download and download a new file. The code 'if os.path.getsize(self.file_path) >117625:' is just my example if the user stops the download. Its not the full size of the file.</p>
  
  <p>thanks.</p>
  
  <pre><code>from ftplib import FTP
  
  class ftpness:
  
      def __init__(self):
              self.connect(myhost, myusername, mypassword)
  
      def handleDownload(self,block):
          self.f.write(block)
          if os.path.getsize(self.file_path) &gt;117625:
                  self.ftp.abort()
  
      def connect(self,host, username, password):
          self.ftp = FTP(host)
          self.ftp.login(username, password)
          self.get(self.file_path)
  
      def get(self,filename):
          self.f = open(filename, 'wb')
          self.ftp.retrbinary('RETR ' + filename, self.handleDownload)
          self.f.close()
          self.ftp.close
  
  a = ftpness()
  </code></pre>