By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,630 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

Partial download with ftplib and retrbinary

P: n/a
I am breaking/interrupting my connection with the ftp server 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.
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On May 24, 11:53 am, fepeac...@googlemail.com wrote:
I am breaking/interrupting my connection with the ftp server 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.
Have you tried ftp.abort()? The docs say "Abort a file transfer that
is in progress. Using this does not always work, but it's worth a
try.".

I'd also suggest that you download more than 1 byte at a time, perhaps
1024 (fewer if you're on dial-up).
Jun 27 '08 #2

P: n/a
On 24 Mag, 12:53, fepeac...@googlemail.com 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
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.