473,289 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Partial download with ftplib and retrbinary

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
2 7127
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Kevin Ollivier | last post by:
Hi all, I've come across a problem that has me stumped, and I thought I'd send a message to the gurus to see if this makes sense to anyone else. =) Basically, I'm trying to upload a series of...
2
by: siggy2 | last post by:
Hi, I'm using Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 I've noticed a strange (= not deterministic) behaviour of ftplib.py: sometimes (not always) it fails (after a variable number of...
0
by: Peter A. Schott | last post by:
Got a strange scenario going on here in that I could have sworn this worked yesterday. I am issuing binary retrieval calls to an FTP server, writing to a file, close the file, then removing the...
5
by: Richard Lewis | last post by:
Hi there, I'm having a problem with unicode files and ftplib (using Python 2.3.5). I've got this code: xml_source = codecs.open("foo.xml", 'w+b', "utf8") #xml_source = file("foo.xml",...
2
by: Harlin Seritt | last post by:
Using ftplib from Python I am trying to get all files in a particular directory using ftplib and then send those same files to another ftp server. I have tried using commands like 'get *' and 'mget...
1
by: Ëåîíîâ Àëåêñåé | last post by:
Hello! I use this code: from ftplib import FTP def handleDownload(block): file.write(block) print "." file = open('1', 'wb') ftp = FTP('ftp.utk.ru')
5
by: John Salerno | last post by:
I'm experimenting with this now and I'm a little confused about transferring commands. This might be more of an FTP question than strictly Python, but it's still related to how to use the ftplib...
1
by: aum | last post by:
Hi, I've been having some headaches with FTP.retrbinary() hanging on completion. I'm only seeing the symptom when downloading a 700k .bmp file in passive mode. What happens is that after...
12
by: johnny | last post by:
I have taken a look at the code that dose one download at time, in multi threaded manner: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/465531 What I wanted to do is, make it download...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.