Multiple FTP download using Muliti thread
Justin Ezequiel
justin.mailinglists at gmail.com
Mon Dec 4 04:56:33 EST 2006
import ftplib, posixpath, threading
from TaskQueue import TaskQueue
def worker(tq):
while True:
host, e = tq.get()
c = ftplib.FTP(host)
c.connect()
try:
c.login()
p = posixpath.basename(e)
fp = open(p, 'wb')
try: c.retrbinary('RETR %s' % e, fp.write)
finally: fp.close()
finally: c.close()
tq.task_done()
if __name__ == '__main__':
q = TaskQueue()
host = 'ftp.microsoft.com'
c = ftplib.FTP(host)
c.connect()
try:
c.login()
folder = '/deskapps/kids/'
for n in c.nlst(folder):
if n.lower().endswith('.exe'):
q.put((host, n))
finally: c.close()
numworkers = 4
for i in range(numworkers):
t = threading.Thread(target=worker, args=(q,))
t.setDaemon(True)
t.start()
q.join()
print 'Done.'
More information about the Python-list
mailing list