-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathftp_threading.py
More file actions
53 lines (41 loc) · 1.2 KB
/
ftp_threading.py
File metadata and controls
53 lines (41 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import threading
import Queue
import time
from ftplib import FTP
import os
class workerThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
print "In worker thread"
while True:
host = self.queue.get()
try:
ftp = FTP(host)
ftp.login()
print "listing files in root directory"
files = ftp.dir()
directory = "/"
filematch = '*'
ftp.cwd(directory)
for filename in ftp.nlst(filematch):
print 'Getting ' + filename
except Exception as e:
print e
self.queue.task_done()
queue = Queue.Queue()
def main():
for i in range(5):
print "creating workerThread : %d" % i
worker = workerThread(queue)
worker.setDaemon(True)
worker.start()
print "WorkerThread %d created" % i
with open('ftp_server.list') as f:
for eachline in f:
server_name = eachline.strip('\n')
queue.put(server_name)
queue.join()
print "All task over !!"
main()