<div>hello, everyone:</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; I&#39;m new to twisted and ftp protocol. for some purpose, i need a python ftp client to do list, get, put, remove&nbsp;operations&nbsp;upon a FTP server which is implemented with twisted. </div>
<div>&nbsp;&nbsp;&nbsp; here is my codes, testing for&nbsp;&#39;get&#39; passed, but &#39;put&#39; failed. i checked the api of storeFile, abd got these:</div>
<div>&nbsp;</div>
<div>&#39;&#39;&#39;</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>&nbsp;</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 &#39;STOR&#39; 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>&#39;&#39;&#39;</div>
<div>&nbsp;but i don&#39;t know how to handle it, :)</div>
<div>&nbsp;sorry for the rubbish codes, but i really need help from you guys, it&#39;s hard to find some useful info from twisted official doc,</div>
<div>&nbsp;Any suggestion&nbsp;on how to&nbsp;write a nice ftp client with twisted is welcome.&nbsp;</div>
<div>&nbsp;Thanks in advance. </div>
<div>&nbsp;</div>
<div>IOSCAS</div>
<div>&nbsp;</div>
<div>______________________________________________________________________________</div>
<div>&nbsp;</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>&nbsp;</div>
<div>class SaveFile(Protocol):<br>&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp; save the ftp file to local filesystem<br>&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp; def __init__(self, output):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.fout = open(output, &#39;w&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; def dataReceived(self, data):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.fout.write(data)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.fout.close()</div>
<div>&nbsp;</div>
<div>class FTP:<br>&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;</div>
<div>&nbsp;&nbsp;&nbsp; a simple ftp client</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp; def __init__(self, host, port):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;&#39;&#39; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; init <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.__host = host<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.__port = port<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.__username = &#39;aaa&#39;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.__passwd = &#39;bbb&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.__passive =&nbsp;1</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; def __get(self, ftp_client, src, des):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; save_file = SaveFile(des)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = ftp_client.retrieveFile(src, save_file)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = ftp_client.quit()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d.addCallback(lambda result: reactor.stop())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return d</div>
<div>&nbsp;&nbsp;&nbsp;</div>
<div>&nbsp;&nbsp; &nbsp;def get(self, src, des):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get src file from ftp server, store it in des<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; creator = ClientCreator(reactor, FTPClient, self.__username, self.__passwd, self.__passive)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; defer = creator.connectTCP(self.__host, self.__port).addCallback(self.__get, src, des)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reactor.run()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return defer.result&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; def __put(self, ftp_client, src, des):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; source_file = os.path.basename(src)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; target_dir = os.path.dirname(des)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftp_client.changeDirectory(target_dir)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = ftp_client.storeFile(src)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = ftp_client.quit()</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d.addCallback(lambda result: reactor.stop())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return d</div>
<div>&nbsp;&nbsp;&nbsp;</div>
<div>&nbsp; &nbsp;def put(self, src, des):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; put src file to ftp server, store it in des<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; creator = ClientCreator(reactor, FTPClient, self.__username, self.__passwd, self.__passive)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; defer = creator.connectTCP(self.__host, self.__port).addCallback(self.__put, src, des)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reactor.run()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return defer.result</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp;</div>