[Twisted-Python] FTPClient storeFile help.

Oisin Mulvihill oisin at enginesofcreation.ie
Tue Oct 7 06:20:06 MDT 2003


Jp Calderone wrote:

>On Fri, Oct 03, 2003 at 11:18:28AM +0100, Oisin Mulvihill wrote:
>  
>
>>Hi,
>>
>>[snip]
>>Why are there two deferred objects returned by storeFile? The documentation
>>says:
>>
>>storeFile (self ,path ):
>>
>>   Store a file at the given path.
>>
>>   This method issues the 'STOR' FTP command.
>>
>>   Returns:
>>
>>   A tuple of two Deferreds:
>>       
>>   * Deferred IFinishableConsumer . You must call the finish method on the
>>       IFinishableConsumer when the file is completely transferred.
>>
>>   * Deferred list of  control-connection responses.
>>
>>This really doesn't help me much. I get the impression that storeFile
>>isn't sending the contents of file, because its waiting for me to stream
>>the file contents somehow. Maybe via IFinishableConsumer?
>>    
>>
>
>  Correct, I believe.  It looks like you can use FileSender (from t.p.basic)
>if you're just sending something off disk (or from any file object, really)
>here.
>
>  
>
>>In my current program, using ftplib, I do the following to upload a file:
>>
>>   :
>>   ftp.storbinary('STOR %s' % (filename), fp)
>>   :
>>
>>This has the filename and a file descriptor passed to it. Is something
>>like this needed with storeFile?
>>
>>Please can someone give me an answer to this, as looking at the code for
>>twisted.protocols.ftp isn't helping much either. This function seems to be
>>a mystery to me and I want to understand how to use it properly.
>>
>>    
>>
>
>  Try...
>
>    from twisted.protocols.basic import FileSender
>
>    def sendfile(consumer, fileObj):
>        s = FileSender()
>        return s.beginFileTransfer(fileObj, consumer
>            ).addCallback(lambda _: consumer.finish()
>            )
>
>    dC, dL = p.storeFile("path")
>    dC.addCallback(sendfile, fileObj)
>
>  Jp
>  
>
Thanks for replying. I've tried what you've suggested, however I'm
still getting the code "waiting" when the transfer is supposed to happen.
The follow is the code I added based on your suggestion (attached
is the complete source):

<snip>
:
:

def sendfile(consumer, fileObj):
    """
    """
    print "2.1 consumer, fileObject -", consumer, fileObj
    s = FileSender()
    print "2.2"
    d = s.beginFileTransfer(fileObj, consumer)
    print "2.3"
    d.addCallback(lambda _: consumer.finish())
    print "2.4"
    return d


def connectionMade(ftpClient):
    """
    """
    try:
        print "1.0"
        my_file = StringIO("Hello! Did this get sent to the remote side?")
        print "1.1"
        my_filename = 'myfile.txt'   
        print "1.2"
        ftpClient.pwd().addCallbacks(success, fail)
        print "1.3"
        dC, dL = ftpClient.storeFile(my_filename)
        print "1.4"
        dC.addCallback(sendfile, my_file)
        print "1.5"
        dL.addCallbacks(success, fail)
        print "1.6"
       
    except:
        print "Exception -", sys.exc_value
        reactor.stop()

:
:
</snip>

When I run this I get as far as the line:

    d = s.beginFileTransfer(fileObj, consumer)

This is where it now seems to be hanginig. The file transfer doesn't appear
to be happening. I noticed from the docs on beginFileTransfer(), that
it takes 3 args: file object, consumer & transform function. I tried 
adding a
transform function to see it gets called with the data, read from the file,
however this never occured.

I'm not really sure where to go from here. Any suggestions?

Thanks,

om

-- 
Oisin Mulvihill
Engines Of Creation

Email: oisin at enginesofcreation.ie
Work: +353 1 6791602
Mobile: +353 868191540


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ftpclient.py
URL: </pipermail/twisted-python/attachments/20031007/f4b57d5c/attachment.ksh>


More information about the Twisted-Python mailing list