473,406 Members | 2,867 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,406 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 7143
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.