[Twisted-Python] How to setup a ftpserver, ftpclient?

Andrew Bennetts andrew-twisted at puzzling.org
Mon Apr 8 05:00:52 MDT 2002


On Mon, Apr 08, 2002 at 12:10:23PM +0200, Joan Torres wrote:
> Hi,
>  I'm trying to setup a FTPserver, FTPClient for sending a File Transfer.
>  I found a FTPClient.py example in ./doc/examples. I've modified the example
>  and add "ftpClent.retrieveFile('file_name', fileProtocol). What I really
>  don't know is how to setup the fileProtocol and also the FTPserver part.
> 
>  I suppose this may be easy, but I'm new in twisted.
>  Could someone help me?

I'm the guy who wrote the FTPClient class, so blame me if it's too hard :)

First I'll mention that the FTPClient in CVS has much better documentation
than the last release, so you might want to use that.

Are you familiar with the idea of a Protocol?  If not, have a look at the
Writing Servers how-to:
    http://twistedmatrix.com/documents/howto/servers

The idea is that as the file is retrieved, you can use any arbitrary
Protocol you like to process it.  For simply saving to disk, that could be:

----
from twisted.protocols.protocol import Protocol

class FileWriterProtocol(Protocol):
    def __init__(self, filename):
        self.file = open(filename, 'w')

    def dataReceived(self, data):
        self.file.write(data)
----

You could then instantiate this class and pass it as the second argument to
retrieveFile.

I probably should improve the ftpclient.py example to cover this, as it is a
fairly typical sort of use I imagine.  Incidentally, does anyone know if
there is a FileWriterProtocol or similar already in Twisted somewhere?  I
can't think of one off the top of my head.

-Andrew.





More information about the Twisted-Python mailing list