473,614 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7191
On May 24, 11:53 am, fepeac...@googl email.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...@googl email.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('TY PE 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(chun k)
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
7827
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 files via FTP. I'm using ftplib to do it, and for each file I'm using transfercmd("STOR " + myfile) to get the socket, then uploading 4096 bytes at a time and providing status updates via a GUI interface. Finally, I close the socket, set it to...
2
2244
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 minutes from 15 to 130) downloading a 150 MB BINARY file (a big gzipped ascii file) with the traceback reported below. IMVHO this is not a timeout error because my script import
0
1824
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 file from the remote site. When I do this, I end up with 0 byte files. I was hoping to avoid parsing a list of remote and local files and matching them up that way because it would be easier to remove on successful retrieve. I'm including some...
5
6897
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", 'w+b') ftp.retrbinary("RETR foo.xml", xml_source.write)
2
9558
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 *' with no success. I am using the following: srcFtp = FTP(srcHost) srcFtp.login(srcUser, srcPass) srcDir = srcFtp.nlst('.')
1
2989
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
5615
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 methods. Anyway, if what I want to do is send a command to change the file permission settings of a file (or files), I assume I would use transfercmd() and the command would be something like SITE CHMOD 755 name.py, for example. What I'm...
1
6065
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 all the data comes in over the data connection, ..retrbinary() does 'return self.voidresp()'. This has a call chain ending
12
6177
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 multiple files at the same time. I am new to python and have gone over "Dive In To Python" yesterday. Can some give a idea what I need to do. Once I understand the big picture, then I can piece together.
0
8576
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8275
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8429
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7091
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6088
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5538
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4121
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1423
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.