<div>hello, everyone:</div>
<div> </div>
<div> 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. </div>
<div> here is my codes, testing for 'get' passed, but 'put' failed. i checked the api of storeFile, abd got these:</div>
<div> </div>
<div>'''</div>
<div>
<div class="functionHeader"><em><font color="#00cccc">def storeFile(self, path, offset=0): </font></em><a class="functionSourceLink" href="http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/protocols/ftp.py#L2389"><em><font color="#00cccc">(source) </font></em></a></div>
<div class="functionHeader"><em><font color="#00cccc"></font></em> </div>
<div class="functionHeader"><em><font color="#00cccc">Store a file at the given path.</font></em></div>
<div class="functionBody">
<div><em><font color="#00cccc">This method issues the 'STOR' FTP command. </font></em>
<table class="fieldTable">
<tbody>
<tr class="fieldStart">
<td class="fieldName"><font color="#00cccc" size="2"><em>Returns</em></font></td>
<td colspan="2"><font color="#00cccc" size="2"><em>A tuple of two </em></font><a href="http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferred.html"><code><font color="#00cccc" size="2"><em>Deferred</em></font></code></a><font color="#00cccc" size="2"><em>s: </em></font>
<ul>
<li><a href="http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferred.html"><code><font color="#00cccc" size="2"><em>Deferred</em></font></code></a><font color="#00cccc" size="2"><em> </em></font><a href="http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IFinishableConsumer.html"><code><font color="#00cccc" size="2"><em>IFinishableConsumer</em></font></code></a><font color="#00cccc" size="2"><em>. You must call the <code>finish</code> method on the IFinishableConsumer when the file is completely transferred. </em></font>
<li><a href="http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferred.html"><code><font color="#00cccc" size="2"><em>Deferred</em></font></code></a><em><font color="#00cccc"><font size="2"> list of control-connection responses.</font> </font></em></li>
</li></ul></td></tr></tbody></table></div></div></div>
<div>'''</div>
<div> but i don't know how to handle it, :)</div>
<div> 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,</div>
<div> Any suggestion on how to write a nice ftp client with twisted is welcome. </div>
<div> Thanks in advance. </div>
<div> </div>
<div>IOSCAS</div>
<div> </div>
<div>______________________________________________________________________________</div>
<div> </div>
<div>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, defer</div>
<div> </div>
<div>class SaveFile(Protocol):<br> '''<br> save the ftp file to local filesystem<br> '''<br> def __init__(self, output):<br> self.fout = open(output, 'w')<br> <br>
def dataReceived(self, data):<br> self.fout.write(data)<br> self.fout.close()</div>
<div> </div>
<div>class FTP:<br> '''</div>
<div> a simple ftp client</div>
<div> '''<br> def __init__(self, host, port):<br> ''' <br> init <br> '''<br> self.__host = host<br> self.__port = port<br> self.__username = 'aaa'<br>
self.__passwd = 'bbb'<br> self.__passive = 1</div>
<div> </div>
<div> def __get(self, ftp_client, src, des):<br> '''<br> '''<br> save_file = SaveFile(des)<br> d = ftp_client.retrieveFile(src, save_file)<br> d = ftp_client.quit()<br>
d.addCallback(lambda result: reactor.stop())<br> return d</div>
<div> </div>
<div> def get(self, src, des):<br> '''<br> get src file from ftp server, store it in des<br> '''<br> creator = ClientCreator(reactor, FTPClient, self.__username, self.__passwd, self.__passive)<br>
defer = creator.connectTCP(self.__host, self.__port).addCallback(self.__get, src, des)<br> reactor.run()<br> return defer.result </div>
<div> </div>
<div> def __put(self, ftp_client, src, des):<br> '''<br> '''<br> source_file = os.path.basename(src)<br> target_dir = os.path.dirname(des)<br> ftp_client.changeDirectory(target_dir)<br>
d = ftp_client.storeFile(src)<br> d = ftp_client.quit()</div>
<div> d.addCallback(lambda result: reactor.stop())<br> return d</div>
<div> </div>
<div> def put(self, src, des):<br> '''<br> put src file to ftp server, store it in des<br> '''<br> creator = ClientCreator(reactor, FTPClient, self.__username, self.__passwd, self.__passive)<br>
defer = creator.connectTCP(self.__host, self.__port).addCallback(self.__put, src, des)<br> reactor.run()<br> return defer.result</div>
<div> </div>
<div> </div>
<div> </div>