python - Python:多个同时请求
问题描述
此 Python 脚本向从网站加载的 URL 发出 GET 请求。txt 文件。然后它将检查“关键字”的响应。如果找到密钥,它将保存在“WorkingSites.txt”中。
一切都很完美,但速度很慢,因为它同时只检查一个 url。同时检查例如 10 个 URL 的最佳和最简单的方法是什么?
你能在下面给我一个例子吗
谢谢
import requests
import sys
if len(sys.argv) != 2:
print "\n\033[34;1m[*]\033[0m python " + sys.argv[0] \
+ ' websites.txt '
exit(0)
targetfile = open(sys.argv[1], 'r')
while True:
success = open('WorkingSites.txt', 'a')
host = targetfile.readline().replace('\n', '')
if not host:
break
if not host.startswith('http'):
host = 'http://' + host
print '\033[34;1m[*]\033[0m Check : ' + host
try:
r = requests.request('get', host, timeout=5,
headers={'Content-Type': 'application/x-www-form-urlencoded'
,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3163.100 Safari/537.36'
})
text = 'KEYWORD'
except:
print '\033[31;1m[-]\033[0m Failed : No Response\n'
pass
continue
if text in r.text:
print '\033[32;1m[+]\033[0m success : ' + host + '\n'
success.write(host + '\n')
else:
print '\033[31;1m[-]\033[0m Failed : ' + host + '\n'
print "\033[34;1m[*]\033[0m Output Saved On : WorkingSites.txt"
解决方案
import concurrent.futures
import requests
import sys
from timeit import default_timer
import psutil
INPUT = 'websites.txt'
OUTPUT = 'WorkingSites.txt'
SUCCESS = open(OUTPUT, 'a')
START_TIME = default_timer()
def fetch(host):
KEYWORD = 'KEYWORD'
try:
with requests.get(host, timeout=5, headers={
'Content-Type':'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3163.100 Safari/537.36'
}) as response:
print('{0} {1} {2} {3}'.format(host, psutil.cpu_percent(), psutil.virtual_memory()[2], "{:5.2f}s".format(default_timer() - START_TIME)))
if (response.status_code == 200 and KEYWORD in response.text):
SUCCESS.write(host+'\n')
return response
except requests.exceptions.RequestException as e:
pass
async def get_data_asynchronous():
with open(INPUT) as fi:
hosts = fi.read().splitlines()
for host in hosts:
if not host.startswith('http'):
host = 'http://' + host
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
loop = asyncio.get_event_loop()
START_TIME = default_timer()
futures = [
loop.run_in_executor(
executor,
fetch,
host,
)
for host in hosts
]
for response in await asyncio.gather(*futures):
pass
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.ensure_future(get_data_asynchronous()))
print("\033[34;1m[*]\033[0m Output Saved On : "+OUTPUT)
main()
这是我目前的脚本,它对第一个 url 非常有效,但随后无论出于何种原因,它都会变慢。
这是我的脚本或电脑的问题吗?有人可以测试一下吗我上传了几千个网址到 pastebin https://pastebin.com/raw/5wtrpcDQ
谢谢你 !!!
推荐阅读
- c# - 在环境阶段更改端点路由
- reactjs - 用 useEffect 和 async 函数反应错误边界,我错过了什么?
- node.js - GET 200 请求陷入循环
- github - 如何更改要在 github 上添加的内容的位置?
- python - Zelle 和 Python - 你能在某个角度显示文本吗?
- c - 为什么我们在编译器设计的词法分析中将字符串视为单个标记?
- python - 对于 (0,1) 范围内的坐标,为什么 OR-Tools 返回零作为总行程长度?
- c# - 转换字典
> 到另一个列表< 字典 > - flutter - 如何让每个用户保存首选项而不是“未来的实例”
' - javascript - 我使用 JS FETCH 加载 json 文件并处理 404(未找到)