473,404 Members | 2,174 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,404 software developers and data experts.

Unable to abort a FTP command?

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

Jul 25 '07 #1
2 3000
On 25 Lug, 09:48, _...@163.com wrote:
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()

Jul 25 '07 #2
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()

Jul 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bob Altman | last post by:
Hi all, I have a WinForms app that has no main window (just a module with a Sub Main). My Main routine calls a subroutine that wants to politely abort the application rather than return to its...
3
by: dr | last post by:
I created a basic service using VS2005. Add OnPowerEvent method as detailed on MSDN and return false to a PowerBroadcastStatus.QuerySuspend notification. The service also has set...
1
by: Terrance | last post by:
I'm trying to create a small messenger program that uses the tcpclient and tcplistenter objects. When I start the application and run the thread that fires the tcplistener; once the client sends...
2
by: tbh | last post by:
in an error path in an aspx script under DotNet 2, IIS6, Win2003 Server I get the following error (which I don't understand) on Respone.End(): {Unable to evaluate expression because the code is...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.