Unable to abort a FTP command?

W

_wdx

Hi,
I write the following script to retrieve a part of a large file
from a FTP site:

import ftplib

class ftp_getter(object):

def __init__(self):
self.handle = ftplib.FTP('ftp_server_address')
self.handle.set_debuglevel(2)
self.login()

def login(self):
self.handle.login('user', 'pass')
self.handle.cwd('/temp1/')

def quit(self, is_close = False):
self.handle.quit()
if is_close:
self.handle.close()
print 'ftp handle closed'

def getpart_callback(self, received):
print "received a packet"
if self.cnt <= 0:
if not self.outf.closed:
self.outf.close()
if not self.aborted:
try:
self.handle.abort()
self.aborted = True
except Exception,ex:
pass
else:
print 'received packet, [0] = %x' % ord(received[0])
self.outf.write(received)
self.cnt -= len(received)

def getpart(self, ftp_filename, rest, cnt, out_filename):
self.outf = open(out_filename, 'wb')
self.cnt = cnt
self.aborted = False
self.handle.retrbinary('RETR ' + ftp_filename,
self.getpart_callback, 8192, rest)
if not self.outf.closed:
self.outf.close()

if __name__ == '__main__':
g = ftp_getter()
g.getpart('FILE_TO_RETRIEVE.DAT', 50000, 20, 'out.dat')
g.quit(True)
print "all done."

As the last four lines shown, I want to connect to my FTP server,
retrieve 20 bytes starting at offset 50000 from FILE_TO_RETRIEVE.DAT,
and stop retrieving after more than 20 bytes have been received. It's
quite simple, but to my suprise, this code does not work.
"self.handle.abort()" have been executed and I got the expected
response from server(426), but the RETR command does not seem to stop
at all. More and more data are received and getpart_callback method is
called again and again.
Why the RETR command is not actually aborted? Can anyone help me?

Thanks

Xu Wang
 
B

billiejoex

Hi,
I write the following script to retrieve a part of a large file
from a FTP site:

import ftplib

class ftp_getter(object):

def __init__(self):
self.handle = ftplib.FTP('ftp_server_address')
self.handle.set_debuglevel(2)
self.login()

def login(self):
self.handle.login('user', 'pass')
self.handle.cwd('/temp1/')

def quit(self, is_close = False):
self.handle.quit()
if is_close:
self.handle.close()
print 'ftp handle closed'

def getpart_callback(self, received):
print "received a packet"
if self.cnt <= 0:
if not self.outf.closed:
self.outf.close()
if not self.aborted:
try:
self.handle.abort()
self.aborted = True
except Exception,ex:
pass
else:
print 'received packet, [0] = %x' % ord(received[0])
self.outf.write(received)
self.cnt -= len(received)

def getpart(self, ftp_filename, rest, cnt, out_filename):
self.outf = open(out_filename, 'wb')
self.cnt = cnt
self.aborted = False
self.handle.retrbinary('RETR ' + ftp_filename,
self.getpart_callback, 8192, rest)
if not self.outf.closed:
self.outf.close()

if __name__ == '__main__':
g = ftp_getter()
g.getpart('FILE_TO_RETRIEVE.DAT', 50000, 20, 'out.dat')
g.quit(True)
print "all done."

As the last four lines shown, I want to connect to my FTP server,
retrieve 20 bytes starting at offset 50000 from FILE_TO_RETRIEVE.DAT,
and stop retrieving after more than 20 bytes have been received. It's
quite simple, but to my suprise, this code does not work.
"self.handle.abort()" have been executed and I got the expected
response from server(426), but the RETR command does not seem to stop
at all. More and more data are received and getpart_callback method is
called again and again.
Why the RETR command is not actually aborted? Can anyone help me?

Thanks

Xu Wang

I would *strongly* rencommend avoid using ABOR.
The easiest way to abort the data transfer is to simply close the data
connection.
Instead of using ftp.retrbinary you could 'handle' the data connetion
('manually') by yourself.
The code below starts RETRieving a file, and quit when more than 2^19
bytes are transmitted (not tested).
Hope this helps.


fd = open('retrieved_file', 'wb')

ftp = ftplib.FTP()
ftp.connect(host=host, port=port)
ftp.login(user=user, passwd=pwd)

# use binary transfer type
ftp.voidcmd('TYPE I')
conn = ftp.transfercmd('retr 1.tmp', rest=None)
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
fd.write(chunk)
bytes_recv += len(chunk)
conn.close()
# here we should get a 426 response
ftp.voidresp()
fd.close()
ftp.close()
 
W

_wdx

Thank you. You are right, retrbinary did not notice I want to abort,
so it won't break the recv loop and close data connection. I changed
getpart and callback like this, now it works:

def getpart_callback(self, received):
print "received a packet"
if self.cnt <= 0:
return True
else:
print 'received packet, [0] = %x' % ord(received[0])
self.outf.write(received)
self.cnt -= len(received)

def getpart(self, ftp_filename, rest, cnt, out_filename):
self.outf = open(out_filename, 'wb')
self.cnt = cnt
self.handle.voidcmd('TYPE I')
conn = self.handle.transfercmd('RETR ' + ftp_filename, rest)
while 1:
data = conn.recv(8192)
if not data:
break
if self.getpart_callback(data):
try:
self.handle.abort()
break
except:
pass
self.outf.close()
self.handle.voidresp()
conn.close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top