[Twisted-Python] need help for twisted FTPClient

柳锴 ioscas at gmail.com
Tue Feb 10 20:43:07 MST 2009


hello, everyone:

    I'm new to twisted and ftp protocol. for some purpose, i need a python
ftp client to do list, get, put, remove operations upon a FTP server which
is implemented with twisted.
    here is my codes, testing for 'get' passed, but 'put' failed. i checked
the api of storeFile, abd got these:

'''
 *def storeFile(self, path, offset=0): **(source)
*<http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/protocols/ftp.py#L2389>
**
*Store a file at the given path.*
 *This method issues the 'STOR' FTP command. *  *Returns* *A tuple of two **
Deferred*<http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferred.html>
*s: *

   - *Deferred*<http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferred.html>
   * **IFinishableConsumer*<http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IFinishableConsumer.html>
   *. You must call the finish method on the IFinishableConsumer when the
   file is completely transferred. *
   - *Deferred*<http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferred.html>
   * list of control-connection responses. *

'''
 but i don't know how to handle it, :)
 sorry for the rubbish codes, but i really need help from you guys, it's
hard to find some useful info from twisted official doc,
 Any suggestion on how to write a nice ftp client with twisted is welcome.
 Thanks in advance.

IOSCAS

______________________________________________________________________________

from twisted.protocols.ftp import FTPClient, FTPFileListProtocol
from twisted.internet.protocol import Protocol, ClientCreator
from twisted.python import usage
from twisted.internet import reactor, defer

class SaveFile(Protocol):
    '''
    save the ftp file to local filesystem
    '''
    def __init__(self, output):
        self.fout = open(output, 'w')

    def dataReceived(self, data):
        self.fout.write(data)
        self.fout.close()

class FTP:
    '''
    a simple ftp client
    '''
    def __init__(self, host, port):
        '''
        init
        '''
        self.__host = host
        self.__port = port
        self.__username = 'aaa'
        self.__passwd = 'bbb'
        self.__passive = 1

    def __get(self, ftp_client, src, des):
        '''
        '''
        save_file = SaveFile(des)
        d = ftp_client.retrieveFile(src, save_file)
        d = ftp_client.quit()
        d.addCallback(lambda result: reactor.stop())
        return d

    def get(self, src, des):
        '''
        get src file from ftp server, store it in des
        '''
        creator = ClientCreator(reactor, FTPClient, self.__username,
self.__passwd, self.__passive)
        defer = creator.connectTCP(self.__host,
self.__port).addCallback(self.__get, src, des)
        reactor.run()
        return defer.result

    def __put(self, ftp_client, src, des):
        '''
        '''
        source_file = os.path.basename(src)
        target_dir = os.path.dirname(des)
        ftp_client.changeDirectory(target_dir)
        d = ftp_client.storeFile(src)
        d = ftp_client.quit()
        d.addCallback(lambda result: reactor.stop())
        return d

   def put(self, src, des):
        '''
        put src file to ftp server, store it in des
        '''
        creator = ClientCreator(reactor, FTPClient, self.__username,
self.__passwd, self.__passive)
        defer = creator.connectTCP(self.__host,
self.__port).addCallback(self.__put, src, des)
        reactor.run()
        return defer.result
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20090211/8d0403f9/attachment.html>


More information about the Twisted-Python mailing list