#!/usr/bin/env python
import threading
import urllib
import sys
import time
import suf

def dirscan_code(url):
    testdir='/xxxyyyxxxyyy.xy'
    try:
        dircode=urllib.urlopen(url.strip()+testdir).getcode()
	if (dircode != 200):
        	return 0
        else:
                return 1
    except IOError:
        print '<-Time Out->'+url.strip()
class dirscan(threading.Thread):
    def __init__(self,urlarr):
        threading.Thread.__init__(self)
        self.urlarr=urlarr
    def run(self):
                urls=self.urlarr
                for url in urls:
			if len(url)<3:
				continue
			if url.find('://')<0:
				url='http://'+url
                        if (dirscan_code(url.strip())==1):
                                print '<-error->'+url.strip()+'     404 page getcode is 200'
                        else:
                                for dir in (dirlist+suf.sufscan(url)):
                                        try:
                                                urlgetcode=str(urllib.urlopen(url.strip()+dir,'HEAD').getcode())
                                                scanurl=url.strip()+dir.strip()
                                                if (urlgetcode=='200'):
                                                        print '\033[32m<--'+urlgetcode+'-->'+scanurl+'\033[0m'
                                                else:
                                                        pass #print '<--'+urlgetcode+'-->'+scanurl
                                        except IOError:
                                                        pass #print '<-Time Out->'+url.strip()
					except KeyboardInterrupt:
							exit
def test():
        thnum=int(len(urllist)/num)+1
        if (num > len(urllist)):
                print 'Error:Maximum thread %i' % (len(urllist))
        else:
                for i in range(num):
                        urlarr.append(urllist[i*thnum:(i+1)*thnum])
                for url in urlarr:
                        tharr.append(dirscan(url))
                for i in range(num):
                        tharr[i].start()
if __name__=='__main__':
        if (len(sys.argv)==4):
                urllist=open(sys.argv[1]).readlines()
                dirlist=open(sys.argv[2]).readlines()
                num=int(sys.argv[3])
                urlarr=[]
                tharr=[]
                test()
        else:
                print        'iEg:%s UrlFile DirFile Threads' % (sys.argv[0])
