#!/usr/bin/python
# encoding: utf-8
import time,sys
import urllib2,httplib
import threading
 
def ReadHost(file):
	hosts=[]
	f=open(file,'r')
	for line in f:
		li=line.strip('\n') 
		hosts.append(line.strip())
	f.close()
	return hosts

class ThreadClass(threading.Thread):
	def __init__(self,host):
		threading.Thread.__init__(self)
		self.host=host
	def run(self):
		global CodeHost
		try:
			res=urllib2.urlopen(self.host).getcode()
			if mutex.acquire(1):
				CodeHost[self.host]=res
				mutex.release()
		except urllib2.HTTPError,e:
			CodeHost[self.host]=e.code
		except Exception,e:
			pass
def MulThreadResolve(fr):
	start=time.ctime()
	print 'starting MulThreadResolve at: ',start
	hosts=ReadHost(fr)
	threads=[]
	for host in hosts:
		t=ThreadClass(host)
		threads.append(t)
	cntHost=len(hosts)
	#cntHost=3
	for i in range(cntHost):
       		threads[i].start()
	for i in range(cntHost):
        	threads[i].join()
 
	print 'ending MulThreadResolve at :', time.ctime(),'\n'
 
if __name__=='__main__':
        CodeHost={}
        mutex=threading.Lock()
	MulThreadResolve('img_dir.txt')
	print "\033[32m %-35s %-10s\033[0m" % ('url','code')
	for k,v in  CodeHost.items():
		if v==200:
			print "\033[32m %-35s %-10s\033[0m" % (k,v)
		if v==403:
			print "\033[33m %-35s %-10s\033[0m" % (k,v)
		if v==404:
			print "\033[31m %-35s %-10s\033[0m" % (k,v)