<HTML>
<HEAD>
<TITLE>Re: [ftputil] How to correctlry close a ftp connection</TITLE>
</HEAD>
<BODY>
<FONT SIZE="4"><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>On 12/15/08 12:28 PM, &quot;Yvan Strahm&quot; &lt;yvan.strahm@gmail.com&gt; wrote:<BR>
<BR>
</SPAN></FONT></FONT><BLOCKQUOTE><FONT SIZE="4"><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>On Sun, Dec 14, 2008 at 10:48 AM, Stefan Schwarzer &lt;sschwarzer@sschwarzer.net&gt; wrote:<BR>
</SPAN></FONT></FONT><BLOCKQUOTE><FONT SIZE="4"><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>Hello,<BR>
<BR>
On 2008-12-12 14:47, Stefan Schwarzer wrote:<BR>
&gt; Regarding the symmetry of the FTPHost construction and calling<BR>
&gt; the close method, at first sight this looks well. However, if the<BR>
&gt; constructor fails, the close method won't be called, so that may<BR>
&gt; be the reason why you exhaust the possible number of connections.<BR>
<BR>
It seems I was a bit hasty here. If the _constructor_ fails, the<BR>
connection wouldn't have been established in the first place, so<BR>
it's not necessary to close it. &quot;Closing&quot; a connection which<BR>
actually isn't there may rather cause an exception _in the<BR>
&quot;close&quot; call_.<BR>
<BR>
More important, thus, usually are exceptions which happen after<BR>
the construction.<BR>
<BR>
&gt; I think you should use a try ... finally construct:<BR>
&gt;<BR>
&gt; try:<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;print &quot;CONNECTING TO &quot;+ ftp + ftp_dir+file+&quot;\n&quot;<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;host=ftputil.FTPHost(ftp,user,password)<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;try:<BR>
<BR>
That's why the &quot;try&quot; comes _after_ calling the constructor. If<BR>
the connection wasn't opened, it doesn't need to be closed.<BR>
<BR>
It's similar in the Python idiom<BR>
<BR>
fobj = open(...)<BR>
try:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;...<BR>
finally:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;fobj.close()<BR>
<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host.chdir(ftp_dir+file)<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;names=host.listdir(host.curdir)<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for name in names:<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if host.path.isfile(name):<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p=&quot;pep&quot;<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pep=re.search(p,name)<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if m_pep:<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returned=name<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host.download_if_newer(name,target,'b')<BR>
<BR>
Besides: Behind the scenes download_if_newer opens a new<BR>
connection to the FTP server in order to make a file object. So<BR>
not only explicit constructor calls generate connections. (There<BR>
shouldn't be connection leaks because of that because unused file<BR>
objects are reused, and they are closed when the FTPHost object<BR>
is closed.)<BR>
<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;finally:<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host.close()<BR>
&gt; except:<BR>
&gt; &nbsp;&nbsp;&nbsp;&nbsp;...<BR>
<BR>
The curious thing is that your problem comes from the code which<BR>
is executed _before_ you call the constructor that fails with an<BR>
exception. :-)<BR>
<BR>
Best regards,<BR>
Stefan<BR>
</SPAN></FONT></FONT></BLOCKQUOTE><FONT SIZE="4"><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
Hello Stefan,<BR>
<BR>
Thanks! the nested try:try:finally:except is doing the trick. I didn't count how many time in my original script the first try was &quot; tried &quot; without success &nbsp;but I suspect that this was the cause of all these not closed connection, as you pointed out in your first reply.<BR>
<BR>
Thanks a lot for your help<BR>
Best regards<BR>
yvan<BR>
&nbsp;<BR>
<BR>
<BR>
</SPAN></FONT></FONT></BLOCKQUOTE><FONT SIZE="4"><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>I haven&#8217;t looked into this yet, but this looks like you could use the with syntax if you&#8217;ve got the right version of python, basically it would do most of the work of the try catch blocks and it looks a little cleaner.<BR>
<BR>
-Richard</SPAN></FONT></FONT>
</BODY>
</HTML>