[Twisted-Python] Client/Server upload problem

Sean Dugan smdugan01 at gmail.com
Wed Jul 21 01:37:14 EDT 2010

I can't seem to figure out how a client uploads a file to the server using
twisted.protocols.ftp. For the server side I am using the ftpserver example
from the twisted website. For the client I have the following:

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

from twisted.protocols.basic import FileSender

# Standard library imports
import string
import sys
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

class BufferingProtocol(Protocol):
    """Simple utility class that holds all data written to it in a
    def __init__(self, fileToWrite):
        self.buffer = StringIO()
        self.fileToWrite = fileToWrite

    def dataReceived(self, data):

# Define some callbacks

def success(response):
    print 'Success!  Got response:'
    print '---'
    if response is None:
        print None
        print string.join(response, '\n')
    print '---'

def fail(error):
    print 'Failed.  Error was:'
    print error

def showFiles(result, fileListProtocol):
    print 'Processed file listing:'
    for file in fileListProtocol.files:
        print '    %s: %d bytes, %s' \
              % (file['filename'], file['size'], file['date'])
    print 'Total: %d files' % (len(fileListProtocol.files))

def showBuffer(result, bufferProtocol):
    print 'Got data:'
    print bufferProtocol.buffer.getvalue()

def writeBufferToFile(result, bufferProtocol):
    f = open(bufferProtocol.fileToWrite, 'w')

class Options(usage.Options):
    optParameters = [['host', 'h', 'localhost'],
                     ['port', 'p', 21],
                     ['username', 'u', 'anonymous'],
                     ['password', None, 'bozo'],
                     ['passive', None, 0],
                     ['debug', 'd', 1],

def run():
    # Get config
    config = Options()
    config.opts['port'] = int(config.opts['port'])
    config.opts['passive'] = int(config.opts['passive'])
    config.opts['debug'] = int(config.opts['debug'])

    # Create the client
    FTPClient.debug = config.opts['debug']
    creator = ClientCreator(reactor, FTPClient, config.opts['username'],

def connectionFailed(f):
    print "Connection Failed:", f

def fileTransferFail(failure):

def cbStore(consumer, filename):
    fs = FileSender()
    d = fs.beginFileTransfer(open(filename, 'r'), consumer)
    d.addCallback(lambda _: consumer.finish()).addErrback(fileTransferFail)
    return d

def connectionMade(ftpClient):
    # Get the current working directory
    ftpClient.pwd().addCallbacks(success, fail)

    # Get a detailed listing of the current directory
    fileList = FTPFileListProtocol()
    d = ftpClient.list('.', fileList)
    d.addCallbacks(showFiles, fail, callbackArgs=(fileList,))

    # Change to the parent directory
    ftpClient.cdup().addCallbacks(success, fail)

    filename = "FtpDownload.txt"
    uploadpath = "FtpUpload.txt"
    d1, d2 = ftpClient.storeFile(uploadpath)
    d1.addCallback(cbStore, filename).addErrback(fileTransferFail)
    d2.addCallback(lambda _: reactor.stop())

# this only runs if the module was *not* imported
if __name__ == '__main__':

When I run this, it returns “Failure: twisted.protocols.ftp.CommandFailed:
['550 FtpUpload.txt: Permission denied']”. I can download a file, and view
the files in a folder, I just can't upload a file. Also if I log-in as one
the users in the .dat file, instead of anonymous, I can't even view the
files in a folder. So is there a way to give anonymous or a user in the .dat
file more permissions, like the write permission?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20100721/3a80b7e5/attachment.htm 

More information about the Twisted-Python mailing list