[Twisted-Python] FTPClient - aborting transfers

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Fri Sep 4 09:29:06 EDT 2009


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.

Jean-Paul



More information about the Twisted-Python mailing list