importhashlibimportaiohttpimportasynciofrom multiprocessing importProcess, Queue, Manager#"""#aiohttp:发送http请求#1.创建一个ClientSession对象#2.通过ClientSession对象去发送请求(get, post, delete等)#3.await 异步等待返回结果#"""
classDirscan():def __init__(self,target):
self.target=target
self.targetmd5= ''self.allqueue=Queue()
self.urlpath=r'字典文件' #字典文件,可以修改
self.Ansdomain =Manager().list()
self.processnum= 8 #进程数 可以修改
self.alldictnum=0defdicturl(self):
with open(self.urlpath,'r',encoding='utf-8') as f:for i inf.readlines():
self.allqueue.put(self.target+'/'+i.strip('\n'))
self.alldictnum=self.allqueue.qsize()
asyncdefmain(self,url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as res:
htmlstr=await res.text()
md5hash= hashlib.md5(htmlstr.encode("utf8"))
md5=md5hash.hexdigest()returnres.status,md5defdirscan(self):
loop=asyncio.get_event_loop()
task=loop.create_task(self.main(self.target))
status, self.targetmd5=loop.run_until_complete(task)while self.allqueue.empty() !=True:
tmp=self.allqueue.get()
task=loop.create_task(self.main(tmp))try:print('\r'+str(int(self.alldictnum)-int(self.allqueue.qsize()))+'/'+str(int(self.alldictnum)),end='')
status,mad5=loop.run_until_complete(task)#print('bad : '+tmp)
if ((status==200) and (mad5!=self.targetmd5)):print('OK :'+tmp)
self.Ansdomain.append(tmp)exceptException as e:print(e)defSetProcess(self):
self.dicturl()
allprocess=[]for i inrange(0, self.processnum):
p= Process(target=self.dirscan, args=())
p.start()
allprocess.append(p)for i inallprocess:
i.join()for i inallprocess:
i.close()if __name__ == '__main__':
obj= Dirscan('http://4399.com')
obj.SetProcess()