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:<br><br>
from twisted.protocols.ftp import FTPClient, FTPFileListProtocol<br>from twisted.internet.protocol import Protocol, ClientCreator<br>from twisted.python import usage<br>from twisted.internet import reactor<br><br>from twisted.protocols.basic import FileSender<br>
<br><br># Standard library imports<br>import string<br>import sys<br>try:<br> from cStringIO import StringIO<br>except ImportError:<br> from StringIO import StringIO<br><br><br>class BufferingProtocol(Protocol):<br>
"""Simple utility class that holds all data written to it in a buffer."""<br> def __init__(self, fileToWrite):<br> self.buffer = StringIO()<br> self.fileToWrite = fileToWrite<br>
<br> def dataReceived(self, data):<br> self.buffer.write(data)<br><br># Define some callbacks<br><br>def success(response):<br> print 'Success! Got response:'<br> print '---'<br> if response is None:<br>
print None<br> else:<br> print string.join(response, '\n')<br> print '---'<br><br><br>def fail(error):<br> print 'Failed. Error was:'<br> print error<br><br>def showFiles(result, fileListProtocol):<br>
print 'Processed file listing:'<br> for file in fileListProtocol.files:<br> print ' %s: %d bytes, %s' \<br> % (file['filename'], file['size'], file['date'])<br>
print 'Total: %d files' % (len(fileListProtocol.files))<br><br>def showBuffer(result, bufferProtocol):<br> print 'Got data:'<br> print bufferProtocol.buffer.getvalue()<br><br>def writeBufferToFile(result, bufferProtocol):<br>
f = open(bufferProtocol.fileToWrite, 'w')<br> f.write(bufferProtocol.buffer.getvalue())<br> f.close()<br><br><br>class Options(usage.Options):<br> optParameters = [['host', 'h', 'localhost'],<br>
['port', 'p', 21],<br> ['username', 'u', 'anonymous'],<br> ['password', None, 'bozo'],<br> ['passive', None, 0],<br>
['debug', 'd', 1],<br> ]<br><br>def run():<br> # Get config<br> config = Options()<br> config.parseOptions()<br> config.opts['port'] = int(config.opts['port'])<br>
config.opts['passive'] = int(config.opts['passive'])<br> config.opts['debug'] = int(config.opts['debug'])<br> <br> # Create the client<br> FTPClient.debug = config.opts['debug']<br>
creator = ClientCreator(reactor, FTPClient, config.opts['username'],<br> config.opts['password'], passive=config.opts['passive'])<br> creator.connectTCP(config.opts['host'], config.opts['port']).addCallback(connectionMade).addErrback(connectionFailed)<br>
reactor.run()<br><br>def connectionFailed(f):<br> print "Connection Failed:", f<br> reactor.stop()<br><br>def fileTransferFail(failure):<br> failure.printTraceback()<br> reactor.stop()<br> <br>def cbStore(consumer, filename):<br>
fs = FileSender()<br> d = fs.beginFileTransfer(open(filename, 'r'), consumer)<br> d.addCallback(lambda _: consumer.finish()).addErrback(fileTransferFail)<br> return d<br><br><br>def connectionMade(ftpClient):<br>
# Get the current working directory<br> ftpClient.pwd().addCallbacks(success, fail)<br><br> # Get a detailed listing of the current directory<br> fileList = FTPFileListProtocol()<br> d = ftpClient.list('.', fileList)<br>
d.addCallbacks(showFiles, fail, callbackArgs=(fileList,))<br><br> # Change to the parent directory<br> ftpClient.cdup().addCallbacks(success, fail) <br><br> filename = "FtpDownload.txt"<br> uploadpath = "FtpUpload.txt"<br>
d1, d2 = ftpClient.storeFile(uploadpath)<br> d1.addCallback(cbStore, filename).addErrback(fileTransferFail)<br> d2.addCallback(lambda _: reactor.stop())<br><br><br># this only runs if the module was *not* imported<br>
if __name__ == '__main__':<br> run()<br><br>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?<br>
<div style="visibility: hidden; display: inline;" id="avg_ls_inline_popup"></div><style type="text/css">#avg_ls_inline_popup { position:absolute; z-index:9999; padding: 0px 0px; margin-left: 0px; margin-top: 0px; width: 240px; overflow: hidden; word-wrap: break-word; color: black; font-size: 10px; text-align: left; line-height: 13px;}</style>