Interrupting ftplib.storbinary()

billiejoex gnewsg at gmail.com
Sun Apr 29 18:19:32 EDT 2007


On 26 Apr, 16:29, Florian  Demmer <fdem... at gmail.com> 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='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()




More information about the Python-list mailing list