使用Muliti线程进行多个FTP下载 [英] Multiple FTP download using Muliti thread
问题描述
我已经看过一次下载一次下载的代码,用
多线程方式:
http://aspn.activestate.com/ASPN/Coo.../Recipe/465531
我想做的是,让它下载多个文件同时
时间。我是python的新手,昨天已经过了Dive In To Python
。有些人可以知道我需要做什么。一旦我理解了大局,那么我就能拼凑起来。
I have taken a look at the code that dose one download at time, in
multi threaded manner:
http://aspn.activestate.com/ASPN/Coo.../Recipe/465531
What I wanted to do is, make it download multiple files at the same
time. I am new to python and have gone over "Dive In To Python"
yesterday. Can some give a idea what I need to do. Once I understand
the big picture, then I can piece together.
推荐答案
导入ftplib,posixpath ,穿线
来自TaskQueue导入TaskQueue
def worker(tq):
而True:
主持人,e = tq.get()
c = ftplib.FTP(主持人)
c.connect()
尝试:
c.login()
p = posixpath.basename(e)
fp = open(p,''wb'')
尝试:c.retrbinary(''RETR%s''%e,fp.write)
finally:fp.close()
:c.close()
tq.task_done()
if __name__ ==''__ main __'':
q = TaskQueue()
host =''ftp.microsoft.com''
c = ftplib.FTP(host)
c.connect()
试试:
c.login()
folder =''/ deskapps / kids / ''
$ c $ b for n in c.nlst(folder):
if n.lower()。endswith(''。exe'') :
q.put((主持人,n))
最后:c.close()
numworkers = 4
for i in range(numworkers):
t = threading.Thread(target = worker,args =(q,))
t.setDaemon (真)
t.start()
q.join()
print''完成。'''br />
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.''
Justin Ezequiel写道:
Justin Ezequiel wrote:
来自TaskQueue import TaskQueue
from TaskQueue import TaskQueue
这是什么Python版本?
< / F>
what Python version is this ?
</F>
Fredrik Lundh写道:
Fredrik Lundh wrote:
Justin Ezequiel写道:
Justin Ezequiel wrote:
来自TaskQueue导入TaskQueue
from TaskQueue import TaskQueue
这是什么Python版本?
< / F>
what Python version is this ?
</F>
oops。忘了注意......
http://aspn.activestate.com/ASPN/Coo.../Recipe/475160
oops. forgot to note...
http://aspn.activestate.com/ASPN/Coo.../Recipe/475160
这篇关于使用Muliti线程进行多个FTP下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!