[Twisted-Python] FTPClient - aborting transfers

Matt Bennett matt at bennett.name
Wed Sep 9 09:28:26 EDT 2009

On Fri, Sep 4, 2009 at 2:29 PM, <exarkun at twistedmatrix.com> wrote:
> On 1 Sep, 10:51 pm, matt at bennett.name wrote:
> >Hello all,
> >
> >I've got an FTPClient implementation very similar to the one at [1],
> >and I've added the ability to retrieve a file from the server with the
> >following snippet:
> >
> >class FileReceiver(Protocol):
> >    """
> >        Protocol subclass that writes received data to a local file.
> >    """
> >    def __init__(self, filename):
> >        self.file = open(filename, 'wb')
> >
> >    def dataReceived(self, data):
> >        self.file.write(data)
> >
> >receiver = FileReceiver(local_path)
> >d = ftpClient.retrieveFile(remote_path,receiver)
> >
> >I want to be able to abort the transfers if they've not completed
> >after a specific period of time. When the timeout is triggered, I call
> >ftpClient.transport.loseConnection() which seems to stop the dL on
> >line 109 of [1] from firing, but the data transfer continues until
> >completion. This is true for both storing and retrieving files.
> >
> >In the storing case, I thought that
> >twisted.protocols.basic.FileSender's stopProducing method might help,
> >but all it seems to do is trigger the errback for anyone waiting for a
> >response - the actual data transfer continues.
> >
> >How do I abort the transfer and clear the wire when the timeout fires?
> The DTP connection is set as the (undocumented) dtpInstance attribute on
> the FTP instance it's associated with.  You can call loseConnection on
> that object's transport and the download should stop.  It might be
> worthwhile for someone to clean this up a bit.  Does it ever make sense
> for the transfer to continue when the control channel is gone?  Even if
> so, it'd be nice to have a supported (or at least documented) way to do
> this cleanup.

The only reference to dtpInstance I can find in
twisted/protocols/ftp.py exists on the FTP class. I may be confused,
but I thought this class was the protocol for FTP servers, rather than

In my attempt to find and stop *something* on the FTPClient, I ended
up calling finish() on the IFinishableConsumer passed into
FileSender's send method. This seems to stop the transfer in the store
case, but unfortunately I'm no closer to a solution when retrieving a

Am I missing something?


> Jean-Paul
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

More information about the Twisted-Python mailing list