更新代码

master v1.0
bettleChen 11 months ago
parent 8c94d49a17
commit cf3b9050d0

@ -1,24 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="network.db" uuid="c2716c2d-ad8d-45c8-9b7d-8ee175d220a5">
<data-source source="LOCAL" name="network.db" uuid="aa19178e-f329-4100-9da5-6399b622c4ba">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/../网络仿真/NetworkAnalog/network.db</jdbc-url>
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/NetworkAnalog/network.db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
<data-source source="LOCAL" name="network" uuid="366f0f05-fea8-48a5-b951-96924aa39605">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:D:\头哥\NetworkAnalog\datas\network.db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
<libraries>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.40.1/org/xerial/sqlite-jdbc/3.40.1.0/sqlite-jdbc-3.40.1.0.jar</url>
</library>
</libraries>
</data-source>
</component>
</project>

@ -205,7 +205,7 @@ class NetWorkAnalog(Canvas):
for tag_id, tag in self.AllSimObjs.items():
self.tag_bind(tag.ObjID, "<Button-3>", lambda event, tag=tag: show_menu(event, tag)) # 绑定右击事件
def bind_event(self, component, name):
def add_sim_obj(self, component, name):
# todo: 绑定事件
"""
绑定事件
@ -305,9 +305,9 @@ class NetWorkAnalog(Canvas):
conn_obj.ConfigCorrect = key[1]
self.tag_bind_event()
def show_obj(self, AllSimObj, conns):
def show_obj(self, AllSimObj, AllSimConn):
self.AllSimObjs = AllSimObj
self.conns = conns
self.conns = AllSimConn
for key, sim_obj in self.AllSimObjs.items():
sim_obj.create_img()
for conn in self.conns:
@ -517,7 +517,7 @@ class NetWorkAnalog(Canvas):
addr_en.grid(row=1, column=3, padx=10)
def commit():
self.chose_obj.config(datas)
self.message.show_message(f"组件 {self.chose_obj} 网络配置成功!")
self.message.show_message(f"组件 {self.chose_obj.ObjLabel} 网络配置成功!")
child_r.destroy()
tk.Button(ifs_frame_YN, text='确定', font=('黑体', 16), height=1, command=commit).grid(row=0, column=0,
@ -855,42 +855,50 @@ class NetWorkAnalog(Canvas):
sticky='e', pady=10)
def create_config_button(self):
font_size = 16
font_style = ("Arial", font_size)
style = ttk.Style()
style.configure("TMenubutton", font=font_style)
# 创建一个菜单栏,这里我们可以把他理解成一个容器,在窗口的上方
menubar = tk.Menu(root)
menubar = tk.Menu(root, font=font_style)
# 定义一个空菜单单元
setMenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='删除与修改', menu=setMenu)
setMenu = tk.Menu(menubar, tearoff=0, font=font_style)
menubar.add_cascade(label='删除与修改', menu=setMenu, font=font_style)
setMenu.add_command(label='删除对象', command=self.delete_obj)
setMenu.add_command(label='删除连接线', command=self.delete_line)
setMenu.add_command(label='修改标签', command=self.update_tag_name)
# # 定义一个空菜单单元
setMenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='基础配置', menu=setMenu)
setMenu = tk.Menu(menubar, tearoff=0, font=font_style)
menubar.add_cascade(label='基础配置', menu=setMenu, font=font_style)
setMenu.add_command(label='网络配置', command=self.network_config)
setMenu.add_command(label='路由表配置', command=self.router_table_config)
setMenu.add_command(label='交换表配置', command=self.mac_table_config)
root.config(menu=menubar)
# 定义一个空菜单单元
setMenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='显示设置', menu=setMenu)
setMenu = tk.Menu(menubar, tearoff=0, font=font_style)
menubar.add_cascade(label='显示设置', menu=setMenu, font=font_style)
setMenu.add_command(label='显示/不显示标签', command=self.show_label)
setMenu.add_command(label='显示/不显示接口', command=self.show_interface)
setMenu.add_command(label='显示网络配置信息', command=self.show_network_config)
setMenu.add_command(label='显示路由/交换表信息', command=self.show_router_config)
# 定义一个空菜单单元
setMenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='发送数据包', menu=setMenu)
setMenu = tk.Menu(menubar, tearoff=0, font=font_style)
menubar.add_cascade(label='发送数据包', menu=setMenu, font=font_style)
setMenu.add_command(label='发送数据包', command=self.send_packet)
setMenu.add_command(label='批量发送数据包', command=self.send_packet_list)
menubar.add_command(label='清除屏幕', command=self.clear_canvas)
menubar.add_command(label='导出数据', command=self.export_data)
menubar.add_command(label='导入数据', command=self.import_data)
menubar.add_command(label='校验', command=self.check_network)
menubar.add_command(label='清除屏幕', command=self.clear_canvas, font=font_style)
menubar.add_command(label='导出数据', command=self.export_data, font=font_style)
menubar.add_command(label='导入数据', command=self.import_data, font=font_style)
menubar.add_command(label='校验', command=self.check_network, font=font_style)
# 设置字体大小
for menu in menubar.winfo_children():
menu.config(font=font_style)
for item in menu.winfo_children():
item.config(font=font_style)
root.config(menu=menubar)
def create_widget(self):
# todo: 创建页面
"""
创建整体页面布局
:return:
@ -911,10 +919,10 @@ class NetWorkAnalog(Canvas):
hub = self.create_image(img_height, split_width * 3, image=self.hub_img, anchor="nw")
self.create_text(text_height, split_width * 4 + 40, text="主机", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字
host = self.create_image(img_height, split_width * 4, image=self.host_img, anchor="nw")
self.bind_event(router, "路由器")
self.bind_event(switch, "交换机")
self.bind_event(hub, "集线器")
self.bind_event(host, "主机")
self.add_sim_obj(router, "路由器")
self.add_sim_obj(switch, "交换机")
self.add_sim_obj(hub, "集线器")
self.add_sim_obj(host, "主机")
if __name__ == '__main__':

@ -303,9 +303,17 @@ class AllSimConnect():
nodex_update_sql = f"""
update conn_config set
ip='{node.interface[ifs - 1]["ip"]}',
mac='{node.interface[ifs - 1]["mac"]}',
conn_port='{node.interface[ifs - 1]["conn_port"]}',
addr='{node.interface[ifs - 1]["addr"]}'
mac='{node.interface[ifs - 1]["mac"]}'
"""
if not node.interface[ifs - 1]["conn_port"] == "NULL":
nodex_update_sql += f"""
,conn_port='{int(node.interface[ifs - 1]["conn_port"])}'
"""
if not node.interface[ifs - 1]["addr"] == "NULL":
nodex_update_sql += f"""
,addr='{node.interface[ifs - 1]["addr"]}'
"""
nodex_update_sql += f"""
where node_id='{node.ObjID}' and node_ifs={ifs}
"""
execute_sql(nodex_update_sql)
@ -319,7 +327,8 @@ class AllSimConnect():
"""
execute_sql(update_sql)
except Exception as E:
pass
print(E)
self.canvas.message.show_message(f"{E}")
def check_config(self):
"""

Binary file not shown.

@ -0,0 +1,21 @@
from sqlalchemy import create_engine
engine = create_engine("mysql+pymysql://root:123123@127.0.0.1:3306/test")
import pandas as pd
import pymysql
conn = pymysql.connect(host="127.0.0.1",
port=3306,
user="root",
password="123123")
# 获取游标
cursor = conn.cursor()
cursor.execute("create database test;")
conn.commit()
data: pd.DataFrame = pd.read_excel("step1/data.xlsx")
data.to_sql("test", con=engine)
cursor.execute("use test")
cursor.execute("select * from test")
print(cursor.fetchall())

@ -1,34 +0,0 @@
import sqlite3
import pandas as pd
import os
import xlwt
from PIL import Image
class ExportUtil():
def __init__(self, path):
self.conn = sqlite3.connect('./network.db')
self.path = path
def get_table_names(self):
cursor = self.conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") # 如果你使用SQLite数据库
tables = cursor.fetchall()
cursor.close()
return [table[0] for table in tables]
def export(self):
tables = self.get_table_names()
with pd.ExcelWriter(self.path, engine='openpyxl') as writer:
for table in tables:
table_name = table
# a. 从数据库中获取表的数据并存储在DataFrame中
query = f"SELECT * FROM {table_name}"
df = pd.read_sql(query, self.conn)
# b. 使用Pandas将数据写入Excel文件的不同sheet中
df.to_excel(writer, sheet_name=table_name, index=False)
export = ExportUtil("./data.xlsx")
export.export()

@ -1,18 +1,43 @@
from tkinter import Tk
from NetworkAnalog import *
from SimObjs import *
if __name__ == '__main__':
Tk()
AllSimObjs = {}
host1 = SimHost(None, 50, 100)
AllSimObjs[host1.ObjID] = host1
host2 = SimHost(None, 200, 100)
AllSimObjs[host2.ObjID] = host2
router = SimRouter(None, 100, 100)
AllSimObjs[router.ObjID] = router
AllSimObj = [
{"ObjID": "SHO1", "ObjType": 1, "ObjLabel": "A", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SHO2", "ObjType": 1, "ObjLabel": "B", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SHO3", "ObjType": 1, "ObjLabel": "C", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SHO4", "ObjType": 1, "ObjLabel": "D", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SHO5", "ObjType": 1, "ObjLabel": "E", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SHO6", "ObjType": 1, "ObjLabel": "F", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SHO7", "ObjType": 1, "ObjLabel": "G", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SHO8", "ObjType": 1, "ObjLabel": "H", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SRO1", "ObjType": 2, "ObjLabel": "R1", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SRO2", "ObjType": 2, "ObjLabel": "R2", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SRO3", "ObjType": 2, "ObjLabel": "R3", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SRO4", "ObjType": 2, "ObjLabel": "R4", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SRO5", "ObjType": 2, "ObjLabel": "R5", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SRO6", "ObjType": 2, "ObjLabel": "R6", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SRO7", "ObjType": 2, "ObjLabel": "R7", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SWO1", "ObjType": 3, "ObjLabel": "S1", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
{"ObjID": "SHub1", "ObjType": 4, "ObjLabel": "Hub1", "ObjX": 100, "ObjY": 100, "ConfigCorrect": 0},
]
host1_router_connect = AllSimConnect(None, host1, 1, router, 1)
host2_router_connect = AllSimConnect(None, host2, 1, router, 2)
AllSimConn = [
{"ConnObjID": "conn1", "NobjS": "SHO1", "IfsS": 1, "NobjE": "SRO1", "IfsE": 1, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SHO2", "IfsS": 1, "NobjE": "SRO1", "IfsE": 2, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO2", "IfsS": 1, "NobjE": "SRO1", "IfsE": 4, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SWO1", "IfsS": 1, "NobjE": "SRO1", "IfsE": 3, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SHO2", "IfsS": 1, "NobjE": "SWO1", "IfsE": 2, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO2", "IfsS": 2, "NobjE": "SRO4", "IfsE": 1, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO2", "IfsS": 3, "NobjE": "SRO3", "IfsE": 1, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO3", "IfsS": 2, "NobjE": "SRO4", "IfsE": 2, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO3", "IfsS": 3, "NobjE": "SRO5", "IfsE": 1, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO4", "IfsS": 4, "NobjE": "SHO5", "IfsE": 1, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO4", "IfsS": 3, "NobjE": "SRO5", "IfsE": 2, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO5", "IfsS": 3, "NobjE": "SHO6", "IfsE": 1, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO5", "IfsS": 4, "NobjE": "SRO6", "IfsE": 2, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO6", "IfsS": 3, "NobjE": "SHO3", "IfsE": 2, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO6", "IfsS": 1, "NobjE": "SRO7", "IfsE": 2, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO7", "IfsS": 1, "NobjE": "SHub1", "IfsE": 1, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SRO7", "IfsS": 3, "NobjE": "SHO4", "IfsE": 1, "ConfigCorrect": 0},
{"ConnObjID": "conn1", "NobjS": "SHub1", "IfsS": 2, "NobjE": "SHO4", "IfsE": 1, "ConfigCorrect": 0},
]

@ -1,33 +0,0 @@
from NetworkAnalog import NetWorkAnalog
from SimObjs import *
if __name__ == '__main__':
AllSimObj = {}
root = Window()
root.title('网络拓扑图')
root_attr = {
"width": root.winfo_screenwidth() * 0.83,
"height": root.winfo_screenheight() * 0.85,
}
size = '%dx%d+%d+%d' % (root_attr['width'], root_attr['height'], (root.winfo_screenwidth() - root_attr['width']) / 2,
(root.winfo_screenheight() - root_attr['height']) / 2 - 30)
canvas = NetWorkAnalog(root, width=root_attr['width'], heigh=root_attr['height'], bg="white")
canvas.place(x=0, y=0, anchor='nw')
root.geometry(size)
host1 = SimHost(canvas, 406, 268)
AllSimObj[host1.ObjID] = host1
host2 = SimHost(canvas, 323, 285)
AllSimObj[host2.ObjID] = host2
router = SimRouter(canvas, 216, 263)
AllSimObj[router.ObjID] = router
host1_router_connect = AllSimConnect(canvas, host1, 1, router, 1)
host2_router_connect = AllSimConnect(canvas, host2, 1, router, 2)
host1.connections[0] = host1_router_connect
router.connections[0] = host1_router_connect
host2.connections[0] = host2_router_connect
router.connections[1] = host2_router_connect
conns = [host1_router_connect, host2_router_connect]
canvas.show_obj(AllSimObj, conns)
root.mainloop()
Loading…
Cancel
Save