代码拉取完成,页面将自动刷新
from flask import Flask, request, render_template
from flask import *
# 解决跨域问题,因为前台是pycharm开启的,后台是flask开启
from flask_cors import CORS
from werkzeug.utils import secure_filename
from flask_socketio import SocketIO
from src import flowdect
import os
from src import utils as ut
from src import capflow
from src import featuredpkt as fdpkt
import time
app = Flask(__name__)
CORS(app, supports_credentials=True)
# flask处理路由:一个响应对应一个路径
@app.route('/')
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/classify')
def page_classify():
return render_template('page_classify.html')
@app.route('/display')
def page_display():
return render_template('page_display.html')
def fileinfo():
list_dirs = os.walk("./sniffer")
flis = []
for root, dirs, files in list_dirs:
for f in files:
full_path = os.path.join(root, f)
file_stat = os.stat(full_path)
size = file_stat.st_size
tm = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(file_stat.st_ctime))
flis.append([1,"./sniffer/"+f,size,tm])
return flis
@app.route('/sniffer')
def page_sniffer():
result = fileinfo()
return render_template('page_sniffer.html',result=result)
@app.route('/update_progress',methods=['POST'])
def update_grogress():
if request.method == 'POST':
#f = request.values.get("mform")
val = ut.get_progress()
return {'update':val}
@app.route('/upload_file', methods=['POST'])
def upload_file():
if request.method == 'POST':
file_obj = request.files.getlist('pcaps')
#f = request.files.get('file1')
storagename = 'train.csv'
for f in file_obj:
file_name = f.filename
ext = os.path.splitext(file_name)[1]
if ext==".csv":
with open(f'./temp/train/{file_name}','wb') as tf:
tf.write(f.read())
ut.set_value('train',f'./temp/{storagename}')
else:
with open(f'./temp/train/{file_name}','wb') as tf:
tf.write(f.read())
ut.set_value('train','pcap')
return 'file uploaded successfully'
@app.route('/train', methods=['POST'])
def model_train():
params = []
if request.method == 'POST':
epochs1 = request.values.get('epochs1')
epochs2 = request.values.get('epochs2')
batchsize = request.values.get('batchsize')
params = [int(epochs1),int(epochs2),int(batchsize)]
data = []
train_csv = r'temp\train.csv'
train_info = []
if ut.check_value('train'):
train_csv = ut.get_value('train')
if ut.get_value('train')=='pcap':
train_csv = fdpkt.get_feature_train(f'./temp/train')
train_info = flowdect.create_model(params,train_csv)
else:
train_info = flowdect.create_model(params,r'temp\train.csv')
if ut.get_value('train')=='pcap':
tps = ut.get_csv_data(f'./temp/train_ipport.csv')
features = ut.get_csv_data(f'./temp/train.csv')
nlis = []
n = min(len(tps),len(features))
for i in range(n):
item = tps[i]
item.append(features[i][3])
item.append(features[i][9])
item.append(features[i][14])
item.append(features[i][17])
nlis.append(item)
train_info.extend(nlis)
return train_info
@app.route('/classify', methods=['POST'])
def disp_classify():
if request.method == 'POST':
#f = request.files.get('file1')
file_obj = request.files.getlist('pcaps')
for f in file_obj:
file_name = f.filename
with open(f'./temp/test/{file_name}','wb') as tf:
tf.write(f.read())
#fname = f.filename
ut._init()
ret = flowdect.predict_from_dir()
ut.set_value('pred',ret)
return ret
@app.route('/update',methods=['POST'])
def update():
if request.method == 'POST':
#ret_lis1 = [1,2,3]
ret_pktlen,ret_nums = flowdect.getstate()
ret_data = [ret_pktlen,ret_nums]
return ret_data
@app.route('/disp_update',methods=['POST'])
def disp_update():
if request.method == 'POST':
label = request.values.get('label')
ret_data = flowdect.getdata(label)
#ret_data = [0]
return ret_data
@app.route('/sniffer',methods=['POST'])
def sniffer():
if request.method == 'POST':
ifaces = capflow.get_ifaces()
#ret_data = [0]
return ifaces
@app.route('/sniffer_start',methods=['POST'])
def sniffer_start():
if request.method == 'POST':
iface = request.values.get('iface')
capflow.start_sniffer(iface)
return []
@app.route('/sniffer_stop',methods=['POST'])
def sniffer_stop():
if request.method == 'POST':
finfo = capflow.stop_sniffer()
finfo.extend(capflow.gettuple(finfo[0]))
#print(finfo)
return finfo
@app.route('/download_file/<path:findex>')
def download_file(findex):
print(findex)
#fname = ut.getfileindex(int(findex))
fname = findex
#fname = "flow_data_20230624191622.pcap"
fpath = "./sniffer/" + fname
return send_file(fpath,as_attachment=True)
@app.route('/delete',methods=['POST'])
def delete():
if request.method == 'POST':
fname = request.values.get('fname')
fpath = "./sniffer/" + fname
ut.delfile(fname)
return "success"
def fileHandler(input):
flowdect.run(input)
def init():
ut.del_files(r'temp/train')
ut.del_files(r'temp/test')
ut.del_files(r'sniffer')
ut.del_files(r'pcaps')
ut._init()
ut.set_value('nums',[0]*10)
if __name__ == '__main__':
init()
app.run(host='0.0.0.0', port='25245')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。