Multiple FTP download using Muliti thread
johnny
rampeters at gmail.com
Mon Dec 4 17:15:21 EST 2006
Where or What folder does the ftp files get downloaded to?
Justin Ezequiel wrote:
> 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