服务端:
# 解封ip接口
@csrf_exempt
def unblock(request):
uname=request.session.get("username","")
user_exist=bkuser.objects.filter(bkuser_name=uname)
admin_exist=bkadmin.objects.filter(bkadmin_name=uname)
if user_exist:
return redirect("/testcenter/")
elif admin_exist:
if request.method=="POST":
ipid=request.POST.get("ipid")
ipinfo=ipset.objects.get(id=ipid)
ipallinfo=iplist.objects.filter(ip_info=ipinfo.dis_ip)
ipinfo.delete()
for ipone in ipallinfo:
ipone.ip_status=1
ipone.save(update_fields=["ip_status"])
return HttpResponse('{"ipcode":"success"}',content_type='application/json')
else:
return redirect("/bosslogin/")
# 封ip接口
@csrf_exempt
def blockip(request):
uname=request.session.get("username","")
user_exist=bkuser.objects.filter(bkuser_name=uname)
admin_exist=bkadmin.objects.filter(bkadmin_name=uname)
if user_exist:
return redirect("/testcenter/")
elif admin_exist:
if request.method=="POST":
inputip=request.POST.get("lockip")
inputday=request.POST.get("addday")
ipinfo=iplist.objects.filter(ip_info=inputip)
if ipinfo and inputday!="":
starttime=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
inputday=int(inputday)
endtime=datetime.datetime.now()+datetime.timedelta(days = inputday)
ipset.objects.get_or_create(dis_ip=inputip,dis_starttime=starttime,dis_endtime=endtime)
for ipone in ipinfo:
ipone.ip_status=0
ipone.save(update_fields=["ip_status"])
return HttpResponse('{"lockcode":"success"}',content_type='application/json')
else:
return HttpResponse('{"lockcode":"fail"}',content_type='application/json')
else:
return redirect("/bosslogin/")
对应的ajax:
//解封ip
function unblock(ip){
$.ajax({
cache:false,
type:"POST",
url:"/unblock/",
data:{'ipid':ip},
async:true,
beforeSend:function(xhr,settings){
xhr.setRequestHeader("X-CSRFToken","{{csrf_token}}");
},
success:function(data){
if(data.ipcode=="success"){
layer.msg("解封ip成功",{
time:1000
},function(){
window.location.reload();
});
}
}
})
};
//禁封ip
function lockip(){
var ip=document.getElementById("inputip").value;
var day=document.getElementById("inputday").value;
$.ajax({
cache:false,
type:"POST",
url:"/blockip/",
data:{'lockip':ip,'addday':day},
async:true,
beforeSend:function(xhr,settings){
xhr.setRequestHeader("X-CSRFToken","{{csrf_token}}");
},
success:function(data){
if(data.lockcode=="success"){
var tipinfo="封禁"+ip+" "+day+"天成功"
layer.msg(tipinfo,{
time:3000
},function(){
window.location.reload();
});
}else{
layer.msg("请求错误",{
time:1000
},function(){
window.location.reload();
});
}
}
})
};
接口自动化测试平台https://www.o200.cn