将mininet流量数据可视化至前端

news/2024/10/18 3:25:54/

目录

    • 准备工作:
    • 将mininet流量数据输入数据库
    • 流量可视化

准备工作:

创建项目

django-admin startproject mininet_web

在这里插入图片描述

python manage.py runserver 0.0.0.0:8000

在这里插入图片描述
init文件加上:
在这里插入图片描述

settings改数据库,具体看上一篇
在这里插入图片描述
创建第一个app

在这里插入图片描述
models.py是创建数据库中表结构用的
在这里插入图片描述
类名代表了数据库表名,且继承了models.Model,类里面的字段代表数据表中的字段(name),数据类型则由CharField(相当于varchar)、DateField(相当于datetime), max_length 参数限定长度。

settings文件里加入app:
在这里插入图片描述
三部曲,自动创表,并通知项目

sudo python manage.py migrate

在这里插入图片描述
testdb创建在mininet_web最里层文件夹中:

示例代码:
# -*- coding: utf-8 -*-
from django.http import HttpResponsefrom myproject.models import Test# Create your models here.
def testdb(request):test1=Test(name='yyy')test1.save()response=""response1=""list=Test.objects.all()for var in list:response1+=var.name+" "response=response1return HttpResponse("<p>"+response+"<p>")

再稍微改改url文件:
在这里插入图片描述
得到效果:
在这里插入图片描述
可以查看数据库:
在这里插入图片描述

将mininet流量数据输入数据库

在我们的应用的代码中:
加入
for i in range(4): for j in range(4): if flows.objects.filter(source_switch=i,destination_switch=j).exists(): flows.objects.filter(source_switch=i,destination_switch=j).update(flows=int(s[i][j])) else : flows1=flows(source_switch=i,destination_switch=j,flows=int(s[i][j])) flows1.save()
即可时刻更新对应流量数据

流量可视化

记得新建templates文件夹
修改settings文件:
在这里插入图片描述
views。py文件里
对流量数据库进行操作:
在这里插入图片描述

from django.http import HttpResponse
from myproject.models import flows
from django.shortcuts import renderdef hello(request):return HttpResponse("Hello world !")def topology(request):# Fetch the traffic data from the databasetraffic_data = flows.objects.all()# Prepare the data for display# You can extend this part to format the data as neededtopology_data = []for data in traffic_data:topology_data.append({'source_switch': data.source_switch,'destination_switch': data.destination_switch,'traffic_size': data.flows})# Render the data in a template and pass it to the contextcontext = {'topology_data': topology_data}return render(request, 'topology.html', context)

topology.html:

<!DOCTYPE html>
<html>
<head><title>Mininet Topology</title>
</head>
<body><h1>Mininet Topology</h1><table><tr><th>Source Switch</th><th>Destination Switch</th><th>Traffic Size</th></tr>{% for data in topology_data %}<tr><td>{{ data.source_switch }}</td><td>{{ data.destination_switch }}</td><td>{{ data.traffic_size }}</td></tr>{% endfor %}</table>
</body>
</html>

接着改改urls:
在这里插入图片描述
就大功告成:
在这里插入图片描述


http://www.ppmy.cn/news/58104.html

相关文章

Python---闭包,装饰器,设计模式之工厂模式

1. 闭包定义 定义双层嵌套函数&#xff0c; 内层函数可以访问外层函数的变量 将内存函数作为外层函数的返回&#xff0c;此内层函数就是闭包函数 2. 闭包的好处和缺点 优点&#xff1a;不定义全局变量&#xff0c;也可以让函数持续访问和修改一个外部变量 优点&#xff1a…

【SQL】窗口函数及行转列等操作总结

1. 窗口函数 窗口函数的应用 排名问题&#xff1a;每个部门按业绩来排名 topN问题&#xff1a;找出每个部门排名前N的员工进行奖励 窗口函数的语法 <窗口函数> over (partition by <用于分组的列名> order by <用于排序的列名>) <窗口函数>的位置&…

【华为OD机试 2023最新 】最多颜色的车辆(C语言题解 100%)

文章目录 题目描述输入描述输出描述用例题目解析代码思路C语言题目描述 在一个狭小的路口,每秒只能通过一辆车,假设车辆的颜色只有 3 种,找出 N 秒内经过的最多颜色的车辆数量。 三种颜色编号为0 ,1 ,2 请注意:这里是假设3中颜色,实际考试中不止3中颜色!!!!!! …

线程池在Java多线程中的应用

前言 随着计算机硬件和软件技术的不断发展&#xff0c;多线程编程在软件开发中变得越来越常见。然而&#xff0c;使用多线程编程时必须小心谨慎&#xff0c;以确保正确性和可维护性。在这个过程中&#xff0c;线程池成为了一个至关重要的工具。本文将介绍其应用场景、注意事项…

JavaWeb( 二 ) URL

1.4.URL统一资源定位符 URL代表Uniform Resource Locator 统一资源定位符&#xff0c;也叫 URL地址 。是用于标识和定位Web上资源的地址&#xff0c;通常用于在Web浏览器中访问网站和文件。 URL由若干部分组成&#xff0c;scheme:// host : port / path 例如&#xff1a; htt…

武汉大学惯性导航课程合集【2021年秋】1.1惯性导航和惯性器件

重力外力加速度 机械阻尼换为电阻尼&#xff0c;带宽提高取决于控制器响应速度 右方是不平衡跷跷板&#xff0c;测量顶面电容 机械中测量角速度的旋转编码器&#xff0c;测速电机测量的两个看得见实际物体的转子定子相对角速度&#xff0c;但是陀螺是相对于惯性参考系&#xf…

Fabric测试与基础

Fabric官网:Introduction — hyperledger-fabricdocs main documentation 1.测试网络 ./network.sh up #启动./network.sh down #关闭 2.Fabric核心模块 peer:主节点模块&#xff0c;负责存储区块链数据&#xff0c;运行维护链码 orderer:交易打包、排序模块 cryptogen:组织…

DAY 49 tomcat服务配置优化

什么是Tomcat Tomcat是Java 语言开发的&#xff0c;Tomcat 服务器是一个免费的开放源代码的Web应用服务器&#xff0c;是Apache 软件基金会的 Jakarta 项目中的一个核心项目&#xff0c;由Apache、Sun和其他一些公司及人共同开发而成。 Tomcat属于轻量级应用服务器&#xff0…