Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from threading import Thread
- import socket
- import csv
- import os
- def program():
- nThreads = 20
- def runthread(urls,inds,index):
- outfile = open('output' + str(index) + '.csv','wb')
- out = csv.writer(outfile)
- for i in xrange(0,len(urls)):
- url = urls[i]
- try:
- ip = socket.gethostbyname(url)
- except:
- ip = 'NOT AVAILABLE'
- s = '--------- ERROR: ' + url + '\n'
- else:
- s = url + '\n'
- s += 'IP: ' + ip + '\n'
- s += 'row: ' + str(inds[i]) + '\n'
- s += '--------------------------\n'
- print s
- out.writerow([inds[i],url,ip])
- outfile.flush()
- print '--------------------------'
- threadinputs = [[[], []] for i in xrange(0,nThreads)]
- ind = 1
- for row in csv.reader(open('input.csv','rb')):
- thread = (ind-1) % nThreads
- threadinputs[thread][0].append(row[0])
- threadinputs[thread][1].append(ind)
- ind += 1
- threads = []
- for t in threadinputs:
- ind = len(threads)
- threads.append(Thread(target=runthread, args=(t[0],t[1],ind)))
- threads[-1].setDaemon(True)
- threads[-1].start()
- for t in threads:
- t.join()
- vals = []
- for i in xrange(0,nThreads):
- for row in csv.reader(open('output' + str(i) + '.csv','rb')):
- vals.append([int(row[0]),row[1],row[2]])
- os.remove('output' + str(i) + '.csv')
- vals = sorted(vals)
- outwriter = csv.writer(open('output.csv','wb'))
- for val in vals:
- outwriter.writerow(val[1:])
- print "DONE"
- program()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement