title: Python ftplib cannot use STOR in callback function following RETR
body: <p>Here is what I need to accomplish:
   - connect to FTP
   - get contents of test.txt
   - write new contents into test.txt right after getting the results</p>
  
  <p>In the real case scenario I need to get previos modification time, stored in a txt file and then upload to FTP only those files which were modified after that time without checking every file specifically (there are thousands of them, that would be too long).</p>
  
  <p>Here is where I'm stuck.</p>
  
  <pre><code>def continueTest(data, ftp):
      print(data, ftp)
      with open('test.txt', 'w+') as file:
          file.write('test')
      with open('test.txt', 'rb') as file:
          ftp.storbinary('STOR htdocs/test.txt', file)
  
  def test():
      host_data=FTP_HOSTS['planz-norwegian']
      ftp = ftplib.FTP(host=host_data['server'],
                       user = host_data['username'],
                       passwd = host_data['password'])
      print('connected to ftp')
      ftp.retrbinary('RETR htdocs/test.txt', lambda data:continueTest(data, ftp))
  
  
  if __name__=='__main__':
      test()
  </code></pre>
  
  <p>This outputs:</p>
  
  <pre><code>connected to ftp
  b'test' &lt;ftplib.FTP object at 0x0322FAB0&gt;
  Traceback (most recent call last):
    File "C:\Python33\Plan Z Editor SL\redistdb.py", line 111, in &lt;module&gt;
      test()
    File "C:\Python33\Plan Z Editor SL\redistdb.py", line 107, in test
      ftp.retrbinary('RETR htdocs/test.txt', lambda data:continueTest(data, ftp))
    File "C:\Python33\lib\ftplib.py", line 434, in retrbinary
      callback(data)
    File "C:\Python33\Plan Z Editor SL\redistdb.py", line 107, in &lt;lambda&gt;
      ftp.retrbinary('RETR htdocs/test.txt', lambda data:continueTest(data, ftp))
    File "C:\Python33\Plan Z Editor SL\redistdb.py", line 99, in continueTest
      ftp.storbinary('STOR htdocs/test.txt', file)
    File "C:\Python33\lib\ftplib.py", line 483, in storbinary
      with self.transfercmd(cmd, rest) as conn:
    File "C:\Python33\lib\ftplib.py", line 391, in transfercmd
      return self.ntransfercmd(cmd, rest)[0]
    File "C:\Python33\lib\ftplib.py", line 351, in ntransfercmd
      host, port = self.makepasv()
    File "C:\Python33\lib\ftplib.py", line 329, in makepasv
      host, port = parse227(self.sendcmd('PASV'))
    File "C:\Python33\lib\ftplib.py", line 873, in parse227
      raise error_reply(resp)
  ftplib.error_reply: 200 Type set to I.
  </code></pre>
  
  <p>If I don't use STOR in a callback, everything works fine, But then, how am I supposed to get data from RETR command?
  I know possible solutions, but I'm sure there must be a more elegant one:
  - use urllib.request instead of RETR (what if there's no HTTP on the server?)
  - reinitialize FTP connection in callback function (may be slower than expected because of waiting for the server to reconnect)
  - user <code>ftp.set_pasv(False)</code> (callback launches, but the script does not end and cannot use <code>ftp.quit()</code> or <code>ftp.close()</code>)</p>