1 Star 1 Fork 0

yixuan/pcapcreate

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
generate_pcap.py 3.13 KB
一键复制 编辑 原始数据 按行查看 历史
yixuan 提交于 2024-04-02 13:54 . add file
# -- coding: utf-8 --
# Name: generate_pcap.py
# Where:
from flask import Blueprint, render_template, request, redirect, url_for, jsonify, send_from_directory
import os
from pcaps_create import creat_http_pcap, fix_content_length # 确保此路径正确
from http_requests import standard_get, ordinary_post, form_submission
from http_responses import http_response_200, http_response_302, http_response_404, http_response_502
# 创建一个Blueprint对象
generate_pcap = Blueprint('generate_pcap', __name__, template_folder='./')
@generate_pcap.route('/generate_pcap', methods=['GET', 'POST'])
def generate():
template_response = "" # 用于存放选择的模板响应内容
request_body = "" # 用于存放请求体的内容
if request.method == 'POST':
# 处理HTTP请求模板的选择
request_template_name = request.form.get('request_template', '')
if request_template_name == "标准GET":
request_body = standard_get()
elif request_template_name == "普通POST":
request_body = ordinary_post()
elif request_template_name == "FORM提交":
request_body = form_submission()
# 处理HTTP响应模板的选择
template_name = request.form.get('template', '')
if template_name == "模板200":
template_response = http_response_200()
elif template_name == "模板302":
template_response = http_response_302()
elif template_name == "模板404":
template_response = http_response_404()
elif template_name == "模板502":
template_response = http_response_502()
# 处理生成操作
if 'generate' in request.form:
request_body = request.form.get('request_body', '')
response_body = request.form.get('response_body', '')
file_name = request.form.get('file_name', '')
# 执行PCAP文件生成
fixs = fix_content_length(request_body)
file_path = creat_http_pcap(fixs, response_body, pcapname=file_name)
# 提取文件名,假设file_path是完整的文件路径
filename = os.path.basename(file_path)
# 重定向到下载路由
return redirect(url_for('generate_pcap.download_file', filename=filename))
# return redirect(url_for('generate_pcap.generate'))
return render_template('generate_pcap.html', template_response=template_response,
request_body_content=request_body)
@generate_pcap.route('/generate_pcap/template')
def template():
template_name = request.args.get('name')
if template_name == "标准GET":
return standard_get()
elif template_name == "普通POST":
return ordinary_post()
elif template_name == "FORM提交":
return form_submission()
else:
return "模板不存在", 404
@generate_pcap.route('/download/<filename>')
def download_file(filename):
# 假设所有生成的.pcap文件都保存在一个名为'generated_pcaps'的目录下
directory = os.path.join(os.getcwd(), 'pcapss')
return send_from_directory(directory, filename, as_attachment=True)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/yixuan1/pcapcreate.git
git@gitee.com:yixuan1/pcapcreate.git
yixuan1
pcapcreate
pcapcreate
master

搜索帮助