Interrupting ftplib.storbinary()

billiejoex gnewsg at
Mon Apr 30 00:19:32 CEST 2007

On 26 Apr, 16:29, Florian  Demmer <fdem... at> wrote:
> 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='', 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 =
    if not chunk:
        print "finished"
    elif stop:
        print "stopped"

More information about the Python-list mailing list