mirror of
https://github.com/shmilylty/OneForAll.git
synced 2026-08-26 04:47:48 +08:00
不使用回调
This commit is contained in:
+31
-30
@@ -1,7 +1,6 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import functools
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp import ClientSession
|
from aiohttp import ClientSession
|
||||||
from aiohttp.resolver import AsyncResolver
|
from aiohttp.resolver import AsyncResolver
|
||||||
@@ -65,33 +64,35 @@ async def fetch(session, url, semaphore):
|
|||||||
return resp, text
|
return resp, text
|
||||||
|
|
||||||
|
|
||||||
def request_callback(future, index, datas):
|
def deal_results(datas, results):
|
||||||
try:
|
for index, result in enumerate(results):
|
||||||
resp, text = future.result()
|
if isinstance(result, Exception):
|
||||||
except BaseException as e:
|
logger.log('DEBUG', result.args)
|
||||||
logger.log('DEBUG', e.args)
|
datas[index]['reason'] = str(result.args)
|
||||||
datas[index]['reason'] = str(e.args)
|
|
||||||
datas[index]['valid'] = 0
|
|
||||||
else:
|
|
||||||
datas[index]['reason'] = resp.reason
|
|
||||||
datas[index]['status'] = resp.status
|
|
||||||
if resp.status == 400 or resp.status >= 500:
|
|
||||||
datas[index]['valid'] = 0
|
datas[index]['valid'] = 0
|
||||||
else:
|
continue
|
||||||
headers = resp.headers
|
if isinstance(result, tuple):
|
||||||
banner = str({'Server': headers.get('Server'),
|
resp, text = result
|
||||||
'Via': headers.get('Via'),
|
datas[index]['reason'] = resp.reason
|
||||||
'X-Powered-By': headers.get('X-Powered-By')})
|
datas[index]['status'] = resp.status
|
||||||
datas[index]['banner'] = banner
|
if resp.status == 400 or resp.status >= 500:
|
||||||
soup = BeautifulSoup(text, 'lxml')
|
datas[index]['valid'] = 0
|
||||||
title = soup.title
|
|
||||||
head = soup.head
|
|
||||||
if title:
|
|
||||||
datas[index]['title'] = title.text
|
|
||||||
elif head:
|
|
||||||
datas[index]['title'] = head.text
|
|
||||||
else:
|
else:
|
||||||
datas[index]['title'] = text
|
headers = resp.headers
|
||||||
|
banner = str({'Server': headers.get('Server'),
|
||||||
|
'Via': headers.get('Via'),
|
||||||
|
'X-Powered-By': headers.get('X-Powered-By')})
|
||||||
|
datas[index]['banner'] = banner
|
||||||
|
soup = BeautifulSoup(text, 'lxml')
|
||||||
|
title = soup.title
|
||||||
|
head = soup.head
|
||||||
|
if title:
|
||||||
|
datas[index]['title'] = title.text
|
||||||
|
elif head:
|
||||||
|
datas[index]['title'] = head.text
|
||||||
|
else:
|
||||||
|
datas[index]['title'] = text
|
||||||
|
return datas
|
||||||
|
|
||||||
|
|
||||||
async def bulk_get_request(datas, port):
|
async def bulk_get_request(datas, port):
|
||||||
@@ -113,11 +114,11 @@ async def bulk_get_request(datas, port):
|
|||||||
for i, data in enumerate(new_datas):
|
for i, data in enumerate(new_datas):
|
||||||
url = data.get('url')
|
url = data.get('url')
|
||||||
task = asyncio.ensure_future(fetch(session, url, semaphore))
|
task = asyncio.ensure_future(fetch(session, url, semaphore))
|
||||||
task.add_done_callback(functools.partial(request_callback,
|
|
||||||
index=i,
|
|
||||||
datas=new_datas))
|
|
||||||
tasks.append(task)
|
tasks.append(task)
|
||||||
if tasks: # 任务列表里有任务不空时才进行解析
|
if tasks: # 任务列表里有任务不空时才进行解析
|
||||||
await asyncio.wait(tasks) # 等待所有task完成
|
# 等待所有task完成 错误聚合到结果列表里
|
||||||
|
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
new_datas = deal_results(new_datas, results)
|
||||||
|
|
||||||
logger.log('INFOR', f'完成异步进行子域的GET请求')
|
logger.log('INFOR', f'完成异步进行子域的GET请求')
|
||||||
return new_datas
|
return new_datas
|
||||||
|
|||||||
Reference in New Issue
Block a user