Interrupting ftplib.storbinary()

F

Florian Demmer

Hi!

I have a number of ftp uploads running in parallel using
ftplib.storbinary and threading and in case one of them fails I need
to interrupt all the others (but not exit the program completely)...
do you guys have an idea how i could implement the interruption as
cleanly as possible?

thanks!
 
L

Larry Bates

Florian said:
Hi!

I have a number of ftp uploads running in parallel using
ftplib.storbinary and threading and in case one of them fails I need
to interrupt all the others (but not exit the program completely)...
do you guys have an idea how i could implement the interruption as
cleanly as possible?

thanks!
I suspect that you would make a file-like object that has a read
method and pass it to storbinary instead of your normal fp argument.
Then you can signal it to stop gracefully. Haven't tested, but
it should be easy to test.

-Larry
 
B

billiejoex

Hi!

I have a number of ftp uploads running in parallel using
ftplib.storbinary and threading and in case one of them fails I need
to interrupt all the others (but not exit the program completely)...
do you guys have an idea how i could implement the interruption as
cleanly as possible?

thanks!

You could interrupt the transfer by directly using the lower 'ftp-
data' socket instead of the storbinary() method.
Try the following code (not tested):


import ftplib

file = open('file.ext', 'r')
ftp = ftplib.FTP()
ftp.connect(host='127.0.0.1', port=21)
ftp.login(user='user', passwd='passwd')
conn = ftp.transfercmd('stor file.ext', rest=None)

# the 'shared' var. change it to
# 1 when you want to stop the transfer
stop = 0
while 1:
chunk = file.read(8192)
conn.sendall(chunk)
if not chunk:
print "finished"
break
elif stop:
print "stopped"
break
conn.close()
ftp.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,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top