Re: Partial download with ftplib and retrbinary



On 24 Mag, 12:53, fepeac...@xxxxxxxxxxxxxx wrote:
I am breaking/interrupting my connection with theftpserver at
present when doing a partial download of a file. I have a callback
with retrbinary that raises an exception and ends the download. The
problem is that the server is not notified and hangs. I cannot send
any further commands and need to relogin to download further. Is there
a way to still continue downloading without having to login again.

My retrbinary function is:

ftp.retrbinary('RETR '+file, handleDownload,1,bound[0])

where bound[0] is an integer specifying the start byte of the
download.

My callback is:

def handleDownload(block):
    global count,localfile,number
    localfile.write(block)
    if count==number:
            raise(Exception)
    count=count+1

where number specifies the number of bytes to download.

Help would be gratefully received.

The server hangs on because the data connection is left open.
Unfortunately you have no easy way to close the data connection by
using retrbinary.
You have to trick a little bit and keep a reference of the data socket
and manually close it.
The example below starts to retrieve a file and stops when 524288
bytes have been received.
Hope this could help you.


import ftplib
ftp = ftplib.FTP('127.0.0.1', 'user', 'password')
ftp.voidcmd('TYPE I')
conn = ftp.transfercmd('RETR filename') # the data socket
bytes_recv = 0
while 1:
chunk = conn.recv(8192)
# stop transfer while it isn't finished yet
if bytes_recv >= 524288: # 2^19
break
elif not chunk:
break
file.write(chunk)
bytes_recv += len(chunk)
conn.close()


--- Giampaolo
http://code.google.com/p/pyftpdlib
.



Relevant Pages

  • Re: Judge finds that downloads are not necessarily lost sales
    ... download is not a lost sale. ...    Wow, comprehension at last... ... defendant fairly be determined to have lost sales for the copyright ... You're a judge. ...
    (rec.arts.anime.misc)
  • Re: Turning off increasing pitch sound on early SS games.
    ... Have you ever tried to download one? ... people out of business that print and sell service manuals. ... raising background sound. ...   Because the sound was that of a heartbeat. ...
    (rec.games.pinball)
  • Re: This is ridiculous....
    ...    What did you download? ... You can create kill filters per newsgroup, ... your ISP may have a NNTP news server included in ... I managed to download Grabit beta 1.7.1 and have configured it to read ...
    (sci.electronics.repair)
  • Re: Response.WriteBinary -> IE 7 security download blocker issue
    ... PDF, XPS, HTML, or downloadable ZIP file. ... The issues is that when the user selects the ZIP download link the IE 7 ... Browser displays the security bar giving the download warning bar at the top ...     r.ClearContent; ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Response.WriteBinary -> IE 7 security download blocker issue
    ... Os here doesn't seem to be the issue as this problem occures in all these ... Browser displays the security bar giving the download warning bar at the top ...     r.ClearContent; ...
    (microsoft.public.dotnet.framework.aspnet)