diff --git a/Internet/__pycache__/data_crud.cpython-39.pyc b/Internet/__pycache__/data_crud.cpython-39.pyc deleted file mode 100644 index 37c09c1..0000000 Binary files a/Internet/__pycache__/data_crud.cpython-39.pyc and /dev/null differ diff --git a/Internet/__pycache__/data_crud_sqlite.cpython-39.pyc b/Internet/__pycache__/data_crud_sqlite.cpython-39.pyc deleted file mode 100644 index be8fcb8..0000000 Binary files a/Internet/__pycache__/data_crud_sqlite.cpython-39.pyc and /dev/null differ diff --git a/Internet/__pycache__/data_detail.cpython-39.pyc b/Internet/__pycache__/data_detail.cpython-39.pyc deleted file mode 100644 index 822b1d5..0000000 Binary files a/Internet/__pycache__/data_detail.cpython-39.pyc and /dev/null differ diff --git a/Internet/__pycache__/data_dispose_CR.cpython-39.pyc b/Internet/__pycache__/data_dispose_CR.cpython-39.pyc deleted file mode 100644 index f80acbf..0000000 Binary files a/Internet/__pycache__/data_dispose_CR.cpython-39.pyc and /dev/null differ diff --git a/Internet/__pycache__/data_envelop.cpython-39.pyc b/Internet/__pycache__/data_envelop.cpython-39.pyc deleted file mode 100644 index a4d0926..0000000 Binary files a/Internet/__pycache__/data_envelop.cpython-39.pyc and /dev/null differ diff --git a/Internet/__pycache__/data_line.cpython-39.pyc b/Internet/__pycache__/data_line.cpython-39.pyc deleted file mode 100644 index bef0ce2..0000000 Binary files a/Internet/__pycache__/data_line.cpython-39.pyc and /dev/null differ diff --git a/Internet/__pycache__/main_X4.cpython-39.pyc b/Internet/__pycache__/main_X4.cpython-39.pyc deleted file mode 100644 index b3f28ed..0000000 Binary files a/Internet/__pycache__/main_X4.cpython-39.pyc and /dev/null differ diff --git a/Internet/data_crud.py b/Internet/data_crud.py deleted file mode 100644 index c0b4b11..0000000 --- a/Internet/data_crud.py +++ /dev/null @@ -1,143 +0,0 @@ -# 导入模块 -import pymysql - - -# 连接本地数据库 -db = pymysql.connect( - host='localhost', # 例:127.0.0.1 本地数据库地址 - port=3306, # 端口号 - user='root', # 用户id - passwd='123456', # 用户密码 - db='demodb' # 连接的数据库 -) -cursor = db.cursor() -# print(cursor) - - - -# 设置查找simhost表的函数 -def get_simhost_alldata(): - # 获取simhost数据库中所有的数据 - sql = ''' - select * from simhost - ''' - cursor.execute(sql) - # 获取全部数据 - test = cursor.fetchall() - # test = pd.read_sql(sql,db) - host_data_all = [] - for i in test: - # 在这里对数据进行整理... - ID = i[0] - ObjID = i[1] - ObjType = i[2] - ObjLable = i[3] - IPAddr = i[4] - ObjX = i[5] - ObjY = i[6] - MACAddr = i[7] - ObjShow = i[8] - host_data = { - 'ID': ID, - 'ObjID': ObjID, - 'ObjType': ObjType, - 'ObjLable': ObjLable, - 'IPAddr': IPAddr, - 'ObjX': ObjX, - 'ObjY': ObjY, - 'MACAddr': MACAddr, - 'ObjShow': ObjShow - } - host_data_all.append(host_data) - # print(host_data) - # print(test) # 错误 - # cursor.close() # 关闭游标 - # db.close() # 关闭数据库连接 - return host_data_all - -# 建立条件查询将ObjShow==1的值全部获取 -def Select_ObjShow(): - sql = '''select * from simhost where ObjShow=1''' - cursor.execute(sql) - objshow_select = cursor.fetchall() - # print(objshow_select) - objshow_select_all = [] - for i in objshow_select: - # 在这里对数据进行整理... - ID = i[0] - ObjID = i[1] - ObjType = i[2] - ObjLable = i[3] - IPAddr = i[4] - ObjX = i[5] - ObjY = i[6] - MACAddr = i[7] - ObjShow = i[8] - host_data = { - 'ID': ID, - 'ObjID': ObjID, - 'ObjType': ObjType, - 'ObjLable': ObjLable, - 'IPAddr': IPAddr, - 'ObjX': ObjX, - 'ObjY': ObjY, - 'MACAddr': MACAddr, - 'ObjShow': ObjShow - } - objshow_select_all.append(host_data) - # print(objshow_select_all) - return objshow_select_all - -# 设置联合查询ipAddr的地址 -def select_and_ip(id): - data_all = [] - sql = '''select ipaddr.ipaddr from ipaddr inner join simhost on ipaddr.sim_id = simhost.id where simhost.id=%s''' - cursor.execute(sql,id) - data = cursor.fetchall() - for item in data: - # print(item[0]) - data_all.append(item[0]) - - # print(data_all) - return data_all - - -# 创建查询ObjLable列的函数 -def select_ObjLable(): - data_all = [] - sql = ''' - select objlable from simhost - ''' - cursor.execute(sql) - test = cursor.fetchall() - for item in test: - data_all.append(item[0]) - # print(data_all) - return data_all - - -# 在这个位置添加一个修改objshow列的数据函数 -def reve_objshow(): - # 将所有的对象全转换为objshow = 0 - sql = '''update simhost set objshow = 0 where objshow = 1''' - cursor.execute(sql) - db.commit() - return None - - -def reve_after(after_data): - # 将获取的数据进行循环 - for item in after_data: - sql = '''update simhost set objshow = 1 where objlable = %s''' - cursor.execute(sql, item) - db.commit() - return None - - - -if __name__ == '__main__': - # print(get_simhost_alldata()) - # print(select_ObjLable()) - reve_objshow() - after_data = ['A','R1','B'] - reve_after(after_data) \ No newline at end of file diff --git a/Internet/data_crud_sqlite.py b/Internet/data_crud_sqlite.py deleted file mode 100644 index ffb3acc..0000000 --- a/Internet/data_crud_sqlite.py +++ /dev/null @@ -1,197 +0,0 @@ -# 尝试用sqlite3数据库试试 -import sqlite3 -conn = sqlite3.connect('internet.db') -cursor = conn.cursor() - -# 设置创建数据库的函数 -def create_simhost(): - sql = '''DROP TABLE IF EXISTS `simhost`''' - cursor.execute(sql) - sql_simhost = '''CREATE TABLE simhost - ( - id integer primary key , - ObjID varchar(20), - ObjType int, - Objlable varchar(20), - IPAddr varchar(64), - ObjX int, - ObjY int, - MACAddr varchar(64), - ObjShow int - );''' - cursor.execute(sql_simhost) - return None - -#建立数据表ipaddr的函数 -def create_ipaddr(): - sql = '''DROP TABLE IF EXISTS `ipaddr`''' - cursor.execute(sql) - sql_ipaddr = '''CREATE TABLE ipaddr - ( - id integer primary key , - ipaddr varchar(25), - sim_id int, - foreign key (sim_id) references simhost(id) - );''' - cursor.execute(sql_ipaddr) - return None - -# 设置两个插入数据函数 -def insert_simhost(): - # 需要插入的数据 - data_simhost = [ - (1, 'SH01', 1, 'A', '101.11.1.2', 170, 120, 'MAC01', 1), - (2, 'SR01', 2, 'R1', '102.10.1.1', 250, 160, 'MAC02', 1), - (3, 'SR02', 2, 'R2', '102.10.5.1', 370, 130, 'MAC03', 1), - (4, 'SR03', 2, 'R4', '102.10.6.1', 400, 200, 'MAC04', 1), - (5, 'SR04', 2, 'R5', '111.12.1.1', 480, 260, 'MAC05', 1), - (6, 'SH02', 1, 'D', '201.21.1.2', 420, 330, 'MAC06', 1), - (7, 'SHO3', 1, 'B', '101.11.1.8', 250, 240, 'MAC07', 0), - (8, 'SR05', 2, 'R3', '102.10.6.2', 520, 160, 'MAC08', 0), - (9, 'SH04', 1, 'C', '201.21.1.5', 540, 320, 'MAC09', 0), - ] - sql = '''INSERT INTO simhost (id,ObjID,ObjType,Objlable,IPAddr,ObjX,ObjY,MACAddr,ObjShow) - VALUES (?,?,?,?,?,?,?,?,?)''' - cursor.executemany(sql,data_simhost) - conn.commit() - return None - -def insert_ipaddr(): - # 需要插入的数据 - data_ipaddr = [ - (1, '101.11.1.1', 1), - (2, '102.10.1.1', 2), - (3, '102.10.5.1', 3), - (4, '111.12.5.1', 4), - (5, '201.21.1.1', 5), - ] - sql = '''INSERT INTO ipaddr (id,ipaddr,sim_id) VALUES (?,?,?)''' - cursor.executemany(sql,data_ipaddr) - conn.commit() - return None - -# 设置获取数据库中所有数据的函数 -def get_simhost_alldata(): - sql = '''SELECT * FROM simhost''' - cursor.execute(sql) - data = cursor.fetchall() - # print(data) - all_data = [] - for item in data: - ID = item[0]; ObjID = item[1]; ObjType = item[2] - ObjLable = item[3]; IPAddr = item[4]; ObjX = item[5] - ObjY = item[6]; MACAddr = item[7]; ObjShow = item[8] - host_data = { - 'ID': ID,'ObjID': ObjID,'ObjType': ObjType, - 'ObjLable': ObjLable,'IPAddr': IPAddr,'ObjX': ObjX, - 'ObjY': ObjY,'MACAddr': MACAddr,'ObjShow': ObjShow - } - all_data.append(host_data) - return all_data - -# 建立条件查询将ObjShow==1的值全部取出 -def Select_ObjShow(): - sql = '''SELECT * FROM simhost WHERE ObjShow=1''' - cursor.execute(sql) - objshow_data = cursor.fetchall() - # print(objshow_data) - objshow_data_all = [] - for item in objshow_data: - ID = item[0]; ObjID = item[1]; ObjType = item[2] - ObjLable = item[3]; IPAddr = item[4]; ObjX = item[5] - ObjY = item[6]; MACAddr = item[7]; ObjShow = item[8] - host_data = { - 'ID': ID,'ObjID': ObjID,'ObjType': ObjType, - 'ObjLable': ObjLable,'IPAddr': IPAddr,'ObjX': ObjX, - 'ObjY': ObjY,'MACAddr': MACAddr,'ObjShow': ObjShow - } - objshow_data_all.append(host_data) - return objshow_data_all - -# 建立联合查询ipaddr的地址 -def select_and_ip(id): - data_all = [] - sql = '''SELECT ipaddr.ipaddr FROM ipaddr INNER JOIN simhost ON ipaddr.sim_id = simhost.id WHERE simhost.id=?''' - cursor.execute(sql, (id,)) - data = cursor.fetchall() - for item in data: - # print(item[0]) - data_all.append(item[0]) - return data_all - -# 建立查询ObjLable列的函数 -def select_ObjLable(): - data_all = [] - sql = '''SELECT objlable FROM simhost''' - cursor.execute(sql) - data = cursor.fetchall() - for item in data: - data_all.append(item[0]) - # print(data_all) - return data_all - -# 建立这个位置添加一个修改objshow列的数据函数 -def reve_objshow(): - sql = '''UPDATE simhost SET objshow=0 WHERE objshow=1''' - cursor.execute(sql) - conn.commit() - return None - -# 建立一个修改指定数据的函数objshow -def reve_after(after_data): - # 将获取的数据进行循环 - for item in after_data: - sql = '''UPDATE simhost SET objshow=1 WHERE objlable=?''' - cursor.execute(sql, (item,)) - conn.commit() - return None - - -# 建立一个通过查询ObjLable并返回ObjID的函数 -def select_ObjLable_ObjID(objlable): - sql = '''SELECT objx,objy,objid,objshow FROM simhost WHERE objlable=?''' - cursor.execute(sql, (objlable,)) - old_data = cursor.fetchall() - new_data = old_data[0] - # print(new_data) - return new_data - -# 建立函数通过指定的ObjID来查找所有的数据 -def Select_ObjID(array): - array_all_data = [] # 用来获取所有的对应的数据并放入其中 - sql = '''SELECT * FROM simhost WHERE objlable=?''' - for item in array: - cursor.execute(sql, (item,)) - data = cursor.fetchall() - # print(data) - ID = data[0][0];ObjID = data[0][1];ObjType = data[0][2] - ObjLable = data[0][3];IPAddr = data[0][4];ObjX = data[0][5] - ObjY = data[0][6];MACAddr = data[0][7];ObjShow = data[0][8] - host_data = { - 'ID': ID, 'ObjID': ObjID, 'ObjType': ObjType, - 'ObjLable': ObjLable, 'IPAddr': IPAddr, 'ObjX': ObjX, - 'ObjY': ObjY, 'MACAddr': MACAddr, 'ObjShow': ObjShow - } - array_all_data.append(host_data) - return array_all_data - - - -if __name__ == '__main__': - # create_simhost() - # insert_simhost() - # create_ipaddr() - # insert_ipaddr() - # get_data_sinhost() - # print(get_simhost_alldata()) - # print(Select_ObjShow()) - # print(select_and_ip(1)) - # print(select_ObjLable()) - # reve_objshow() - # after_data = ['A', 'R1', 'B'] - # reve_after(after_data) - # select_ObjLable_ObjID('A') - # array = ['A','B'] - # print(Select_ObjID(array)) - pass - diff --git a/Internet/data_detail.py b/Internet/data_detail.py deleted file mode 100644 index 224192d..0000000 --- a/Internet/data_detail.py +++ /dev/null @@ -1,117 +0,0 @@ -# 在这个文件里画出详情 - - -# 创建函数用来画出详情 -import time - - -class Detail_Show(): - def __init__(self, cv, img_detail, move_data): - self.cv = cv - self.move_data = move_data - self.img_detail = img_detail - - def show_detail(self): - # time.sleep(5) - self.detail_data_all = [] - # 通过获取的move_data数据来画出详情表 - for item in self.move_data: - detail_data_x = item[0]+20 - detail_data_y = item[1] - detail_type = item[3] - detail_data = (detail_data_x,detail_data_y,detail_type) - self.detail_data_all.append(detail_data) - self.cv.create_image(detail_data_x,detail_data_y, image=self.img_detail, tags='detail') # 完成 - - return self.detail_data_all - - -# 建立一个用来画出详情的类 - -class Show_green_detail(): - def __init__(self, cv, item): - self.cv = cv - self.item = item - - # 建立画出对象详情的函数 - def draw_detail(self): - # 判断type对象来画出详情表 - if self.item[2] == 1: # 当type对象为1 画出主机详情图 - # 在这里画出详情图 - frist_x = self.item[0] # 获取frist_x - frist_y = self.item[1] # 获取frist_y - # 这里缺少一个删除其他详情的步骤 - # 画出连接详情表的线 - self.cv.delete('line') - self.cv.create_line((frist_x,frist_y),(frist_x+40,frist_y),(frist_x+45,frist_y-30),fill='#94D050',tags='line') - # 画出详情表 - self.detail_true = self.cv.create_rectangle(frist_x+30,frist_y-120,frist_x+180,frist_y-30,outline='#94D050',tags='line') - # 画出相应的绿条数据 - self.cv.create_text(frist_x+50,frist_y-110,text='应用层',tags='line') - self.cv.create_text(frist_x+50,frist_y-87,text='传输层',tags='line') - self.cv.create_text(frist_x+50,frist_y-64,text='IP 层',tags='line') - self.cv.create_text(frist_x+50,frist_y-42,text='链路层',tags='line') - # 画出 右侧绿色和蓝色的类进度条 - # 应用层 - self.cv.create_rectangle(frist_x+177,frist_y-115,frist_x+150,frist_y-100,fill='#94D050',outline='#94D050',tags='line') - self.cv.create_rectangle(frist_x+148,frist_y-115,frist_x+128,frist_y-100,fill='#00B0F0',outline='#00B0F0',tags='line') - # 传输层 - self.cv.create_rectangle(frist_x + 177, frist_y - 95, frist_x + 140, frist_y - 80, fill='#94D050', - outline='#94D050', tags='line') - self.cv.create_rectangle(frist_x + 138, frist_y - 95, frist_x + 113, frist_y - 80, fill='#00B0F0', - outline='#00B0F0', tags='line') - # IP 层 - self.cv.create_rectangle(frist_x + 177, frist_y - 73, frist_x + 130, frist_y - 58, fill='#94D050', - outline='#94D050', tags='line') - self.cv.create_rectangle(frist_x + 128, frist_y - 73, frist_x + 98, frist_y - 58, fill='#00B0F0', - outline='#00B0F0', tags='line') - # 传输层 - self.cv.create_rectangle(frist_x + 177, frist_y - 50, frist_x + 113, frist_y - 35, fill='#94D050', - outline='#94D050', tags='line') - self.cv.create_rectangle(frist_x + 111, frist_y - 50, frist_x + 78, frist_y - 35, fill='#00B0F0', - outline='#00B0F0', tags='line') - elif self.item[2] == 2: # 当type对象为2 画出路由器详情图 - # print(2) - frist_x = self.item[0] # 获取frist_x - frist_y = self.item[1] # 获取frist_y - # 这里缺少一个删除其他详情的步骤 - # 画出连接详情表的线 - self.cv.delete('line') - self.cv.create_line((frist_x, frist_y), (frist_x + 40, frist_y), (frist_x + 45, frist_y - 30), - fill='#94D050', tags='line') - # 画出详情表 如果需要全覆盖则需要添加一个fill - self.detail_true = self.cv.create_rectangle(frist_x + 30, frist_y - 120, frist_x + 180, frist_y - 30, - outline='#94D050', tags='line') - # 画出相应的绿条数据 - self.cv.create_text(frist_x + 50, frist_y - 110, text='应用层', tags='line') - self.cv.create_text(frist_x + 50, frist_y - 87, text='传输层', tags='line') - self.cv.create_text(frist_x + 50, frist_y - 64, text='IP 层', tags='line') - self.cv.create_text(frist_x + 50, frist_y - 42, text='链路层', tags='line') - # 画出 右侧绿色和蓝色的类进度条 - # 应用层 - self.cv.create_rectangle(frist_x + 177, frist_y - 115, frist_x + 150, frist_y - 100, fill='#D3D3D3', - outline='#D3D3D3', tags='line') - self.cv.create_rectangle(frist_x + 148, frist_y - 115, frist_x + 128, frist_y - 100, fill='#D3D3D3', - outline='#D3D3D3', tags='line') - # 传输层 - self.cv.create_rectangle(frist_x + 177, frist_y - 95, frist_x + 140, frist_y - 80, fill='#D3D3D3', - outline='#D3D3D3', tags='line') - self.cv.create_rectangle(frist_x + 138, frist_y - 95, frist_x + 113, frist_y - 80, fill='#D3D3D3', - outline='#D3D3D3', tags='line') - # IP 层 - self.cv.create_rectangle(frist_x + 177, frist_y - 73, frist_x + 130, frist_y - 58, fill='#94D050', - outline='#94D050', tags='line') - self.cv.create_rectangle(frist_x + 128, frist_y - 73, frist_x + 98, frist_y - 58, fill='#00B0F0', - outline='#00B0F0', tags='line') - # 传输层 - self.cv.create_rectangle(frist_x + 177, frist_y - 50, frist_x + 113, frist_y - 35, fill='#94D050', - outline='#94D050', tags='line') - self.cv.create_rectangle(frist_x + 111, frist_y - 50, frist_x + 78, frist_y - 35, fill='#00B0F0', - outline='#00B0F0', tags='line') - return None - - - - -if __name__ == '__main__': - pass \ No newline at end of file diff --git a/Internet/data_dispose_CR.py b/Internet/data_dispose_CR.py deleted file mode 100644 index 58541fc..0000000 --- a/Internet/data_dispose_CR.py +++ /dev/null @@ -1,70 +0,0 @@ -# 导入函数 -from data_crud_sqlite import * - - -'''创建类来处理函数''' -class Show_Right_All(): - def __init__(self,cv,all_data): - self.cv = cv - self.all_data = all_data - - - # 创建函数 来处理获取的数据 - def Show_img(self,img_host,img_router): - self.loca_all = [] # 定义一个数组用来存数据 - # 将获取的所有数据处理 - for item in self.all_data: - ObjX = item['ObjX'] - ObjY = item['ObjY'] - ObjID = item['ObjID'] - loca = (ObjX, ObjY) - self.loca_all.append(loca) - # 显示图片 - if item['ObjType'] == 1: - self.cv.create_image(ObjX, ObjY, image=img_host, tags=ObjID) - # elif item['ObjType'] == 2: - else: - self.cv.create_image(ObjX, ObjY, image=img_router, tags=ObjID) - # 显示ObjLable - if item['ObjLable'] == 'A': - self.cv.create_text(ObjX, ObjY+27, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable']) - self.cv.create_text(ObjX, ObjY-32, text=item['IPAddr'], font=('蔚然雅黑',10,'bold'),fill='#C00000', tags=item['IPAddr']) - elif item['ObjLable'] == 'R1': - self.cv.create_text(ObjX+15, ObjY+25, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable']) - self.cv.create_text(ObjX-67, ObjY, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000', - tags=item['IPAddr']) - # 通过联表查询获取IP地址 - ip_data_R1 = select_and_ip(2) - # print(ip_data_R1) - self.cv.create_text(ObjX+30, ObjY-30, text= ip_data_R1, font=('蔚然雅黑',10,'bold'),fill='#C00000', tags=ip_data_R1) - elif item['ObjLable'] == 'R2': - self.cv.create_text(ObjX-17, ObjY+25, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable']) - self.cv.create_text(ObjX, ObjY - 32, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000', - tags=item['IPAddr']) - elif item['ObjLable'] == 'R4': - self.cv.create_text(ObjX-35, ObjY, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable']) - self.cv.create_text(ObjX+20, ObjY - 29, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000', - tags=item['IPAddr']) - ip_data_R4 = select_and_ip(4) - self.cv.create_text(ObjX - 10, ObjY + 26, text=ip_data_R4, font=('蔚然雅黑', 10, 'bold'), fill='#C00000', - tags=ip_data_R4) - elif item['ObjLable'] == 'R5': - self.cv.create_text(ObjX, ObjY+27, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable']) - self.cv.create_text(ObjX-67, ObjY - 12, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000', - tags=item['IPAddr']) - ip_data_R5 = select_and_ip(5) - self.cv.create_text(ObjX - 67, ObjY + 3, text=ip_data_R5, font=('蔚然雅黑', 10, 'bold'), fill='#C00000', - tags=ip_data_R5) - elif item['ObjLable'] == 'D': - self.cv.create_text(ObjX-31, ObjY+10, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable']) - self.cv.create_text(ObjX, ObjY + 27, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000', - tags=item['IPAddr']) - elif item['ObjLable'] == 'C': - self.cv.create_text(ObjX+25, ObjY-27, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable']) - self.cv.create_text(ObjX, ObjY + 27, text=item['IPAddr'], font=('蔚然雅黑', 10, 'bold'), fill='#C00000', - tags=item['IPAddr']) - elif item['ObjLable'] == 'R3': - self.cv.create_text(ObjX, ObjY-30, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable']) - elif item['ObjLable'] == 'B': - self.cv.create_text(ObjX+30, ObjY+10, text=item['ObjLable'], font=('蔚然雅黑',16,'bold'),fill='#C00000', tags=item['ObjLable']) - return None \ No newline at end of file diff --git a/Internet/data_envelop.py b/Internet/data_envelop.py deleted file mode 100644 index b0bdde6..0000000 --- a/Internet/data_envelop.py +++ /dev/null @@ -1,456 +0,0 @@ -# 在这里实现移动信封并实现,并在实现的同时使最下方和最右方的canvas中显示数据 -import copy -import tkinter as tk -import time -from data_detail import Detail_Show, Show_green_detail -from data_crud_sqlite import * -# 建立全局变量 -num_begin_x = 0 -num_begin_y = 0 -num_end_x = 1 -num_end_y = 1 - -# 建立信封移动并处理输出数据的函数 -class EnvelopMove(): - def __init__(self,cv,img_host,img_router,img_envelop,img_detail,move_data,Objshow_data,AppPackTag_Data, - unpacket,move_number,right_on_text,right_under_text,under_text): - self.cv = cv # 主页面最大的canvas页面 - self.unpacket = unpacket # 这是一个函数,暂时没用 - self.img_host = img_host # 主机png图片 - self.img_router = img_router # png图片 - self.img_detail = img_detail # png图片 - self.img_envelop = img_envelop # 信封的png图片 - self.move_data = move_data # 信封的移动位置的数据集合 数据样式:[(229, 202, 'SH01', 1), (300, 300, 'SR01', 2)] - self.x1 = self.move_data[0][0] # 定义初始信封的x轴位置 - self.y1 = self.move_data[0][1] # 定义初始信封的y轴位置 - self.Begin_x = None # 定义一个用来存储移动数据的x轴值 - self.End_y = None # 定义一个用来存储移动数据的y轴值 - self.num_begin_x = 0 # 定义获取数组的第一个对比值 - self.num_begin_x_2 = 0 - self.num_begin_x_3 = 0 # 第三次 - self.num_end_x = 1 # 定义获取数组的下一个对比值 - self.num_end_x_2 = 1 - self.num_end_x_3 = 1 # 第三次 - self.num_begin_y = 0 # 定义获取数据下的固定位置值0 - self.num_end_y = 1 # 定义获取数据下的固定位置值1 - self.coord_Ture = True # 定义一个判断用来使用croods函数 - self.move_number_frist = 1 # 定义一个移动数据的初始值 - self.move_number_last = move_number # 数据为需要循环移动信封的次数 该数据为最后值 - self.move_number_add = False # 定义一个值用来实现信封循环发送的判断值 - self.move_number_add_2 = False # 第二次循环进入第三次 - self.right_on_text = right_on_text # 发送主机文本框 - self.right_under_text = right_under_text # 接收主机文本框 - self.under_text = under_text # 连续信息数据文本框 - self.Objshow_data = Objshow_data # 发送的数据总体信息 - self.AppPackTag_Data = AppPackTag_Data # 输入数据总体 - self.number_late = 0 # 设置循环输出的例子 - self.number_late_1 = 0 - self.number_late_2 = 0 - self.one_print_1 = True # 判断是否为第一次输出 - self.one_print_2 = True # 第二次 - self.one_print_3 = True - self.last_envelop_number = int(self.AppPackTag_Data['AppPackSize']) % 2048 - - - # 创建一个信封对象 - def main_envelop(self): - '''在这个位置编写发送和接收文本框中的信息''' - data_Send_0 = '发送主机: '+self.Objshow_data[0]['ObjLable']+'\n' - data_Send_1 = 'IP地址: '+self.Objshow_data[0]['IPAddr']+'\n' # 发送主机详情第一行 - data_Send_2 = 'Mac地址: '+self.Objshow_data[0]['MACAddr']+'\n' # 发送主机详情第二行 - data_Send_3 = '发送数据包大小: '+self.AppPackTag_Data['AppPackSize']+'KB'+'\n' # 发送主机详情第三行 - data_Send_4 = '已发送数据包数量: 0'+'\n' # 发送主机详情第四行 - data_Send_5 = '需要发送的数据包总数: '+str(self.move_number_last)+'\n' - self.right_on_text.insert(tk.END, data_Send_0) - self.right_on_text.insert(tk.END, data_Send_1) - self.right_on_text.insert(tk.END, data_Send_2) - self.right_on_text.insert(tk.END, data_Send_3) - self.right_on_text.insert(tk.END, data_Send_4) - self.right_on_text.insert(tk.END, data_Send_5) - data_Req_1 = '接收主机: '+self.Objshow_data[-1]['ObjLable']+'\n' - data_Req_2 = 'IP地址: '+self.Objshow_data[-1]['IPAddr']+'\n' - data_Req_3 = 'Mac地址: '+self.Objshow_data[-1]['MACAddr']+'\n' - data_Req_4 = '接收数据包大小: '+self.AppPackTag_Data['AppPackSize']+'KB'+'\n' - data_Req_5 = '已接收的数据包数量: 0'+'\n' - data_Req_6 = '需要接收的数据包总数: '+str(self.move_number_last)+'\n' - self.right_under_text.insert(tk.END, data_Req_1) - self.right_under_text.insert(tk.END, data_Req_2) - self.right_under_text.insert(tk.END, data_Req_3) - self.right_under_text.insert(tk.END, data_Req_4) - self.right_under_text.insert(tk.END, data_Req_5) - self.right_under_text.insert(tk.END, data_Req_6) - self.envelop_1 = self.cv.create_image(self.x1, self.y1, image=self.img_envelop,tags='envelop') # 在初始位置创建一个信封图标 - self.show_data_move() - if self.move_number_last >= 2: - self.envelop_2 = self.cv.create_image(self.x1, self.y1, image=self.img_envelop,tags='envelop') - if self.move_number_last >= 3: - self.envelop_3 = self.cv.create_image(self.x1, self.y1, image=self.img_envelop,tags='envelop') - return None - - - # 类似建立模拟函数,第一步先建立信封显示 - def show_data_move(self): - '''在这个位置添加一个判断用来设置新的envelop ?暂时存疑''' - if self.coord_Ture: - self.x_1, self.y_1 = self.cv.coords(self.envelop_1) - # print(self.x_1,self.y_1) - if self.one_print_1: - s_index = '5.10' - e_index = '5.11' - self.right_on_text.delete(s_index, e_index) - self.right_on_text.insert(e_index, '1') - data_one_1 = '发送主机:'+self.Objshow_data[0]['ObjLable']+'\n' # 第一次发送主机 - data_one_2 = '接收主机: '+self.Objshow_data[-1]['ObjLable']+'\n' # 第二行 - data_one_3 = '数据包大小: '+self.AppPackTag_Data['AppPackSize']+'KB'+'\n' # 第三行 - self.one_print_1 = False - if self.num_end_x <= (len(self.move_data)-1): - # self.Begin_x 为需要x轴移动的距离 - Begin_x = self.move_data[self.num_end_x][self.num_begin_y]-self.move_data[self.num_begin_x][self.num_begin_y] - # self.End_y 为需要y轴移动的距离 - End_y = self.move_data[self.num_end_x][self.num_end_y]-self.move_data[self.num_begin_x][self.num_end_y] - # 移动信封 - self.cv.move(self.envelop_1, Begin_x/100, End_y/100) - if int(self.move_data[self.num_end_x][self.num_begin_y]) - 2 <= self.x_1 <= int( - self.move_data[self.num_end_x][self.num_begin_y]) + 2 and int( - self.move_data[self.num_end_x][self.num_end_y]) - 2 <= self.y_1 <= int( - self.move_data[self.num_end_x][self.num_end_y]) + 2: - time.sleep(1) - self.num_begin_x = self.num_begin_x + 1 - self.num_end_x = self.num_end_x + 1 - self.move_number_add = True - data_late_1 = '发送对象: '+self.Objshow_data[self.number_late]['ObjLable']+'\n' - data_late_1_1 = '发送对象IP地址: '+self.Objshow_data[self.number_late]['IPAddr']+'\n' - data_late_1_2 = '发送对象Mac地址: '+self.Objshow_data[self.number_late]['MACAddr']+'\n' - data_late_1_3 = '数据包对象: '+'1'+' / '+str(self.move_number_last)+'\n' - if self.move_number_last == 1: - data_late_1_4 = '数据包大小: '+str(self.last_envelop_number)+' KB \n' - else: - data_late_1_4 = '数据包大小: '+'2048 KB \n' - data_late_1_5 = '数据解包中...... \n' - data_late_1_6 = '数据解包完成 \n' - data_late_2 = '接收对象:'+self.Objshow_data[self.number_late+1]['ObjLable']+'\n' - data_late_2_1 = '接收对象IP地址: '+self.Objshow_data[self.number_late+1]['IPAddr']+'\n' - data_late_2_2 = '接收对象Mac地址: '+self.Objshow_data[self.number_late+1]['MACAddr']+'\n' - data_late_2_3 = '数据封包中...... \n' - data_late_2_4 = '数据封包完成 \n' - self.under_text.insert(tk.END, '*' * 15 + '第一个数据包信息' + '*' * 15 + '\n') - self.under_text.insert(tk.END, data_late_1) - self.under_text.insert(tk.END, data_late_1_1) - self.under_text.insert(tk.END, data_late_1_2) - self.under_text.insert(tk.END, data_late_1_3) - self.under_text.insert(tk.END, data_late_1_4) - self.under_text.insert(tk.END, data_late_1_5) - self.under_text.insert(tk.END, data_late_1_6) - self.under_text.insert(tk.END, data_late_2) - self.under_text.insert(tk.END, data_late_2_1) - self.under_text.insert(tk.END, data_late_2_2) - self.under_text.insert(tk.END, data_late_2_3) - self.under_text.insert(tk.END, data_late_2_4) - self.number_late += 1 - self.cv.after(12, self.show_data_move) - if self.move_number_add and self.move_number_last >= 2: - self.show_data_move_2() - elif self.num_end_x == len(self.move_data): - self.start_index = "5.11" - self.end_index = "5.12" - self.right_under_text.delete(self.start_index, self.end_index) - self.right_under_text.insert(self.end_index, '1') - if self.move_number_last == 1: - self.cv.delete('envelop') - self.coord_Ture = False - self.detail_data = Detail_Show(cv=self.cv, img_detail=self.img_detail, move_data=self.move_data).show_detail() - return None - - # 建立移动第二个信封的函数 - def show_data_move_2(self): - if self.coord_Ture: - self.x_2, self.y_2 = self.cv.coords(self.envelop_2) - if self.one_print_2: - s_index = '5.10' - e_index = '5.11' - self.right_on_text.delete(s_index, e_index) - self.right_on_text.insert(e_index, '2') - self.one_print_2 = False - if self.num_end_x_2 <= (len(self.move_data) - 1): - # self.Begin_x 为需要x轴移动的距离 - Begin_x = self.move_data[self.num_end_x_2][self.num_begin_y] - self.move_data[self.num_begin_x_2][ - self.num_begin_y] - # self.End_y 为需要y轴移动的距离 - End_y = self.move_data[self.num_end_x_2][self.num_end_y] - self.move_data[self.num_begin_x_2][ - self.num_end_y] - # 移动信封 - self.cv.move(self.envelop_2, Begin_x / 100, End_y / 100) - if int(self.move_data[self.num_end_x_2][self.num_begin_y]) - 2 <= self.x_2 <= int( - self.move_data[self.num_end_x_2][self.num_begin_y]) + 2 and int( - self.move_data[self.num_end_x_2][self.num_end_y]) - 2 <= self.y_2 <= int( - self.move_data[self.num_end_x_2][self.num_end_y]) + 2: - # time.sleep(0.1) - self.num_begin_x_2 = self.num_begin_x_2 + 1 - self.num_end_x_2 = self.num_end_x_2 + 1 - self.move_number_add_2 = True # 当第二个信封进入到第二个节点时,将数据进入到第三个 - '''在这里添加一段输出数据试试判断试试''' - self.under_text.insert(tk.END, '#' * 15 + '第二个数据包信息' + '#' * 15 + '\n') - data_late_1 = '发送对象: ' + self.Objshow_data[self.number_late_1]['ObjLable'] + '\n' - data_late_1_1 = '发送对象IP地址: ' + self.Objshow_data[self.number_late_1]['IPAddr'] + '\n' - data_late_1_2 = '发送对象Mac地址: ' + self.Objshow_data[self.number_late_1]['MACAddr'] + '\n' - data_late_1_3 = '数据包对象: ' + '2' + ' / ' + str(self.move_number_last) + '\n' - if self.move_number_last == 2: - data_late_1_4 = '数据包大小: ' + str(self.last_envelop_number) + ' KB \n' - else: - data_late_1_4 = '数据包大小: ' + '2048 KB \n' - data_late_1_5 = '数据解包中...... \n' - data_late_1_6 = '数据解包完成 \n' - data_late_2 = '接收对象:' + self.Objshow_data[self.number_late_1 + 1]['ObjLable'] + '\n' - data_late_2_1 = '接收对象IP地址: ' + self.Objshow_data[self.number_late_1 + 1]['IPAddr'] + '\n' - data_late_2_2 = '接收对象Mac地址: ' + self.Objshow_data[self.number_late_1 + 1]['MACAddr'] + '\n' - data_late_2_3 = '数据封包中...... \n' - data_late_2_4 = '数据封包完成 \n' - self.under_text.insert(tk.END, data_late_1) - self.under_text.insert(tk.END, data_late_1_1) - self.under_text.insert(tk.END, data_late_1_2) - self.under_text.insert(tk.END, data_late_1_3) - self.under_text.insert(tk.END, data_late_1_4) - self.under_text.insert(tk.END, data_late_1_5) - self.under_text.insert(tk.END, data_late_1_6) - self.under_text.insert(tk.END, data_late_2) - self.under_text.insert(tk.END, data_late_2_1) - self.under_text.insert(tk.END, data_late_2_2) - self.under_text.insert(tk.END, data_late_2_3) - self.under_text.insert(tk.END, data_late_2_4) - self.number_late_1 += 1 - if self.num_end_x_2 == 5: - self.cv.after(12, self.show_data_move_2) - # time.sleep(1) - if self.move_number_add_2 and self.move_number_last>=3: - self.show_data_move_3() - elif self.num_end_x_2 == len(self.move_data): - self.right_under_text.delete(self.start_index, self.end_index) - self.right_under_text.insert(self.end_index, '2') - if self.move_number_last == 2: - self.cv.delete('envelop') - self.coord_Ture = False - self.detail_data = Detail_Show(cv=self.cv, img_detail=self.img_detail, move_data=self.move_data).show_detail() - return None - - # 建立移动第三个信封的函数 - def show_data_move_3(self): - if self.coord_Ture: - self.x_3, self.y_3 = self.cv.coords(self.envelop_3) - if self.one_print_3: - s_index = '5.10' - e_index = '5.11' - self.right_on_text.delete(s_index, e_index) - self.right_on_text.insert(e_index, '3') - self.one_print_3 = False - if self.num_end_x_3 <= (len(self.move_data) - 1): - # self.Begin_x 为需要x轴移动的距离 - Begin_x = self.move_data[self.num_end_x_3][self.num_begin_y] - self.move_data[self.num_begin_x_3][ - self.num_begin_y] - # self.End_y 为需要y轴移动的距离 - End_y = self.move_data[self.num_end_x_3][self.num_end_y] - self.move_data[self.num_begin_x_3][ - self.num_end_y] - # 移动信封 - self.cv.move(self.envelop_3, Begin_x / 100, End_y / 100) - if int(self.move_data[self.num_end_x_3][self.num_begin_y]) - 2 <= self.x_3 <= int( - self.move_data[self.num_end_x_3][self.num_begin_y]) + 2 and int( - self.move_data[self.num_end_x_3][self.num_end_y]) - 2 <= self.y_3 <= int( - self.move_data[self.num_end_x_3][self.num_end_y]) + 2: - # time.sleep(0.1) - self.num_begin_x_3 = self.num_begin_x_3 + 1 - self.num_end_x_3 = self.num_end_x_3 + 1 - - self.under_text.insert(tk.END, '*' * 15 + '第三个数据包信息' + '*' * 15 + '\n') - data_late_1 = '发送对象: ' + self.Objshow_data[self.number_late_2]['ObjLable'] + '\n' - data_late_1_1 = '发送对象IP地址: ' + self.Objshow_data[self.number_late_2]['IPAddr'] + '\n' - data_late_1_2 = '发送对象Mac地址: ' + self.Objshow_data[self.number_late_2]['MACAddr'] + '\n' - data_late_1_3 = '数据包对象: ' + '2' + ' / ' + str(self.move_number_last) + '\n' - if self.move_number_last ==3 : - data_late_1_4 = '数据包大小: ' + str(self.last_envelop_number) + ' KB \n' - else: - data_late_1_4 = '数据包大小: ' + '2048 KB \n' - data_late_1_5 = '数据解包中...... \n' - data_late_1_6 = '数据解包完成 \n' - data_late_2 = '接收对象:' + self.Objshow_data[self.number_late_2 + 1]['ObjLable'] + '\n' - data_late_2_1 = '接收对象IP地址: ' + self.Objshow_data[self.number_late_2 + 1]['IPAddr'] + '\n' - data_late_2_2 = '接收对象Mac地址: ' + self.Objshow_data[self.number_late_2 + 1]['MACAddr'] + '\n' - data_late_2_3 = '数据封包中...... \n' - data_late_2_4 = '数据封包完成 \n' - self.under_text.insert(tk.END, data_late_1) - self.under_text.insert(tk.END, data_late_1_1) - self.under_text.insert(tk.END, data_late_1_2) - self.under_text.insert(tk.END, data_late_1_3) - self.under_text.insert(tk.END, data_late_1_4) - self.under_text.insert(tk.END, data_late_1_5) - self.under_text.insert(tk.END, data_late_1_6) - self.under_text.insert(tk.END, data_late_2) - self.under_text.insert(tk.END, data_late_2_1) - self.under_text.insert(tk.END, data_late_2_2) - self.under_text.insert(tk.END, data_late_2_3) - self.under_text.insert(tk.END, data_late_2_4) - self.number_late_2 += 1 - if self.num_end_x_3 == 5: - # time.sleep(1) - self.cv.after(12, self.show_data_move_3) - - elif self.num_end_x_3 == len(self.move_data): - self.right_under_text.delete(self.start_index, self.end_index) - self.right_under_text.insert(self.end_index, '3') - if self.move_number_last == 3: - self.cv.delete('envelop') - self.coord_Ture = False - self.detail_data = Detail_Show(cv=self.cv, img_detail=self.img_detail, move_data=self.move_data).show_detail() - return None - - # 在这里创建鼠标左键点击详情头位置显示数据 - def show_detail_all(self, event): - frist_X = event.x - frist_y = event.y - for item in self.detail_data: - if frist_X-10", self.show_detail_all) # 点击鼠标左键触发显示数据函数 - return Mouse - - -# 创建修改objshow列的函数 -def revise_objshow(send_host, reception_host): - '''在这个位置判断发送主机和接收主机的位置并修改其数据''' - if send_host == 'A': - if reception_host == 'B': - # 调用函数修改objshow列为 0 - reve_objshow() - # 建立一个经过对象的数组 - after_data = ['A', 'R1', 'B'] - # 调用函数reve_after来给对象重新赋值 - reve_after(after_data) - '''后面可能需要在建立一个重组对象数组的功能''' - elif reception_host == 'C': - reve_objshow() - after_data = ['A', 'R1', 'R2', 'R4', 'R5', 'C'] - reve_after(after_data) - elif reception_host == 'D': - reve_objshow() - after_data = ['A', 'R1', 'R2', 'R4', 'R5', 'D'] - reve_after(after_data) - elif send_host == 'B': - if reception_host == 'A': - reve_objshow() - after_data = ['B', 'R1', 'A'] - reve_after(after_data) - elif reception_host == 'C': - reve_objshow() - after_data = ['B', 'R1', 'R2', 'R4', 'R5', 'C'] - reve_after(after_data) - elif reception_host == 'D': - reve_objshow() - after_data = ['B', 'R1', 'R2', 'R4', 'R5', 'C'] - reve_after(after_data) - elif send_host == 'C': - if reception_host == 'A': - reve_objshow() - after_data = ['C', 'R5', 'R4', 'R2', 'R1', 'A'] - reve_after(after_data) - elif reception_host == 'B': - reve_objshow() - after_data = ['C', 'R5', 'R4', 'R2', 'R1', 'A'] - reve_after(after_data) - elif reception_host == 'D': - reve_objshow() - after_data = ['C', 'R5', 'D'] - reve_after(after_data) - elif send_host == 'D': - if reception_host == 'A': - reve_objshow() - after_data = ['D', 'R5', 'R4', 'R2', 'R1', 'A'] - reve_after(after_data) - elif reception_host == 'B': - reve_objshow() - after_data = ['D', 'R5', 'R4', 'R2', 'R1', 'B'] - reve_after(after_data) - elif reception_host == 'C': - reve_objshow() - after_data = ['D', 'R5', 'C'] - reve_after(after_data) - return after_data - - -# 将数组顺序倒换并返回倒序数组 -def order_array(array, send_host, reception_host): - if send_host == 'D': - if reception_host == 'A': - array = array[::-1] - elif reception_host == 'B': - ObjID_reception = select_ObjLable_ObjID(reception_host) - ObjID_send = select_ObjLable_ObjID(send_host) - array = array[::-1] - for item in array: - if item == ObjID_send: - index_move_send = array.index(ObjID_send) - array = [ObjID_send]+array[:index_move_send]+array[index_move_send+1:] - elif item == ObjID_reception: - index_move_reception = array.index(ObjID_reception) - array = array[:index_move_reception]+array[index_move_reception+1:]+[ObjID_reception] - else: - pass - elif reception_host == 'C': - ObjID_send = select_ObjLable_ObjID(send_host) - for item in array: - if item == ObjID_send: - index_move_send = array.index(ObjID_send) - array = [ObjID_send]+array[:index_move_send]+array[index_move_send+1:] - else: - pass - elif send_host == 'C': - if reception_host == 'A': - array = array[::-1] - elif reception_host == 'B': - ObjID_reception = select_ObjLable_ObjID(reception_host) - ObjID_send = select_ObjLable_ObjID(send_host) - array = array[::-1] - for item in array: - if item == ObjID_send: - index_move_send = array.index(ObjID_send) - array = [ObjID_send] + array[:index_move_send] + array[index_move_send + 1:] - elif item == ObjID_reception: - index_move_reception = array.index(ObjID_reception) - array = array[:index_move_reception] + array[index_move_reception + 1:] + [ObjID_reception] - else: - pass - elif reception_host == 'D': - ObjID_send = select_ObjLable_ObjID(send_host) - for item in array: - if item == ObjID_send: - index_move_send = array.index(ObjID_send) - array = [ObjID_send] + array[:index_move_send] + array[index_move_send + 1:] - else: - pass - elif send_host == 'B': - if reception_host == 'A': - array = array[::-1] - elif reception_host == 'C' or reception_host == 'D': - ObjID_send = select_ObjLable_ObjID(send_host) - for item in array: - if item == ObjID_send: - index_move_send = array.index(ObjID_send) - array = [ObjID_send] + array[:index_move_send] + array[index_move_send + 1:] - else: - pass - return array - - - -'''建立一个函数使获取信封循环次数并为每次添加编号''' -def add_number(number): - number_all = [] - for i in range(0,number): - number = 'envelop_'+str(i+1) - number_all.append(number) - return number_all - - diff --git a/Internet/data_line.py b/Internet/data_line.py deleted file mode 100644 index 621f5b3..0000000 --- a/Internet/data_line.py +++ /dev/null @@ -1,76 +0,0 @@ - - -# 创建一个画线的类 -class Line_Right(): - def __init__(self, cv, line_data): - self.cv = cv - self.line_data = line_data - - # 创建函数来画出需要的线 - def line_test(self): - # 将数据通过循环取出,使用下标的形式 - for i in range(0, len(self.line_data)): - '''需要进行判断下标i+1存在数据''' - if i + 1 != len(self.line_data): - x = self.line_data[i]['ObjX'] - y = self.line_data[i]['ObjY'] - next_x = self.line_data[i + 1]['ObjX'] - next_y = self.line_data[i + 1]['ObjY'] - self.cv.create_line(x, y, next_x, next_y, width=3, fill='#5B9BD5') - # print(i) - return None - - -# 创建一个用于画出永久固定的无用的线的类 -class Useless_line(): - def __init__(self, cv, Useless_line_data): - self.cv = cv - self.Useless_line_data = Useless_line_data - - - # 创建函数用来画出线 - def draw_line_less(self): - data_all_zero = self.data_handle() - # 通过循环判断获取的对象是谁,在给该对象画出线条 - for item in data_all_zero: - # 在这里添加判断 - if item['ID'] == 7: # 这个以后换一个判断信息 - # 在这里画出线 - self.cv.create_line(item['ObjX'],item['ObjY'],250,160,width=3, fill='#00B050') - if item['ID'] == 8: - # 在这里画出连接三条线 - self.cv.create_line(item['ObjX'],item['ObjY'],370,130,width=1, fill='#5B9BD5') - self.cv.create_line(item['ObjX'], item['ObjY'], 400, 200, width=1, fill='#5B9BD5') - self.cv.create_line(item['ObjX'], item['ObjY'], 480, 260, width=1, fill='#5B9BD5') - if item['ID'] == 9: - self.cv.create_line(item['ObjX'], item['ObjY'], 480, 260, width=2, fill='#5B9BD5') - return None - - # 建立函数用来处理获取的数据 - def data_handle(self): - data_ObjShow_zero_all = [] - for item in self.Useless_line_data: - # print(item) - if item['ObjShow'] == 0: - # print(item) - data_ObjShow_zero_all.append(item) - # print(data_ObjShow_zero_all) - return data_ObjShow_zero_all - - - -# 对获取的line_data进行处理 -def line_draw(ShowData): - line_data_all = [] # 定义一个数组用来存储所有数据 - # 将获取的数据循环获取需要的数据 - for item in ShowData: - # print(item) - ObjX = item['ObjX'] - ObjY = item['ObjY'] - line_data = { - 'ObjX': ObjX, - 'ObjY': ObjY - } - line_data_all.append(line_data) - # print(line_data_all) - return line_data_all diff --git a/Internet/judgement_data.py b/Internet/judgement_data.py deleted file mode 100644 index 8e915b1..0000000 --- a/Internet/judgement_data.py +++ /dev/null @@ -1,157 +0,0 @@ -# 建立一个函数用来判断封包数据的正确性 -''' -在这里编写数据包正误判断 -在这里引入数据 -test: 先使用自身对比判断出错误环节,or与下一层产生的数据对比是否出错。 -判断是否要写成一个函数呢?在数据处理函数中直接判断会不会好一点呢? -目前想法是先判断自己吧。 -自己进行判断的话则不需要额外写外接函数。 -TRUE == 对自身获取的地址进行判断正误、判断格式。 -''' -import re - -# 定义一个正则表达式来判断ip地址的格式是否正确 - -zz_ip = '^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$' -zz_mac = '([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})' -compile_ip = re.compile(zz_ip) -compile_mac = re.compile(zz_mac) - -# 判断是否符合ip地址格式要求 -def Judgement_Zz_IP(IP_Data): - if compile_ip.match(IP_Data): - return True - else: - return False - - -def Judgement_Zz_MAC(MAC_Data): - if compile_mac.match(MAC_Data): - return True - else: - return False - -# 判断AppData数据正误,地址是否存在,判断格式. -def Judgement_AppData(SourceAppAddr, TargetAppAddr): - ''' - 在这里添加判断数据正确性的代码 备注:test 目前 - 不知道需要对比的数据是啥所以先对比自身 - 不建议使用for循环,可以使用if嵌套 - ''' - # 将获取的数据进行切割 - SourceAppAddr = SourceAppAddr.split(':')[0] - TargetAppAddr = TargetAppAddr.split(':')[0] - # print(SourceAppAddr) - source = Judgement_Zz_IP(SourceAppAddr) # 发出数据IP判断 - if source != True: - print('SourceIP地址格式错误') - target = Judgement_Zz_IP(TargetAppAddr) # 接收数据IP判断 - if target != True: - print('TargetIP地址格式错误') - return None - -# 判断TransData数据正误 判断地址是否与上级地址是都一样。是否存在,外加判断端口号是否符合要求是否存在 -def Judgement_TransData(Old_SourceAppAddr,Old_TargetAppAddr,New_SourceAppAddr,New_TargetAppAddr,SentPort,RpctPort): - ''' - 在这里添加判断数据正确性的代码 备注:test 目前 - 不知道需要对比的数据是啥所以先对比自身 - 不建议使用for循环,可以使用if嵌套 - ''' - # 先判断地址是否符合格式在进行比较,先进行字符串的切割 - Old_SourceAppAddr = Old_SourceAppAddr.split(':')[0] - Old_TargetAppAddr = Old_TargetAppAddr.split(':')[0] - New_SourceAppAddr = New_SourceAppAddr.split(':')[0] - New_TargetAppAddr = New_TargetAppAddr.split(':')[0] - new_source = Judgement_Zz_IP(New_SourceAppAddr) - new_target = Judgement_Zz_IP(New_TargetAppAddr) - if new_source: - if Old_SourceAppAddr != New_SourceAppAddr: - print('新地址与原地址不相同') - else: - print('SourceIP地址格式错误') - if new_target: - if Old_TargetAppAddr != New_TargetAppAddr: - print('新地址与原地址不相同') - else: - print('TargetIP地址格式错误') - # 判断端口号是否符合要求 - if SentPort < 2048 or SentPort > 65535: - print('端口号不符合格式') - if RpctPort< 2048 or RpctPort > 65535: - print('端口号不符合格式') - return None - - - -# 判断IPData数据正误 -def Judgement_IPData(Old_SourceIP,Old_TargetIP,New_SourceIP,New_TargetIP): - ''' - 在这里添加判断数据正确性的代码 备注:test 目前 - 不知道需要对比的数据是啥所以先对比自身 - 不建议使用for循环,可以使用if嵌套 - ''' - new_sourceIP = Judgement_Zz_IP(New_SourceIP) - new_targetIP = Judgement_Zz_IP(New_TargetIP) - if new_sourceIP: - if Old_SourceIP != New_SourceIP: - print('新地址与原地址不相同') - else: - print('SourceIP地址格式错误') - if new_targetIP: - if Old_TargetIP != New_TargetIP: - print('新地址与原地址不相同') - else: - print('TargetIP地址格式错误') - return None - - - - -# 判断MACData数据正误 -def Judgement_MACData(Old_SourceIP,Old_TargetIP,New_SourceIP,New_TargetIP,SentMAC,RcptMAC): - ''' - 在这里添加判断数据正确性的代码 备注:test 目前 - 不知道需要对比的数据是啥所以先对比自身 - 不建议使用for循环,可以使用if嵌套 - ''' - new_sourceIP = Judgement_Zz_IP(New_SourceIP) - new_targetIP = Judgement_Zz_IP(New_TargetIP) - if new_sourceIP: - if Old_SourceIP != New_SourceIP: - print('新地址与原地址不相同') - else: - print('SourceIP地址格式错误') - if new_targetIP: - if Old_TargetIP != New_TargetIP: - print('新地址与原地址不相同') - else: - print('TargetIP地址格式错误') - sentmac = Judgement_Zz_MAC(SentMAC) - rcptmac = Judgement_Zz_MAC(RcptMAC) - if sentmac != True: - print('SentMAC地址格式错误') - if rcptmac != True: - print('RcptMAC地址格式错误') - return None - - - -if __name__ == '__main__': - # 在这里建立一组test数据用来比较 - # test: 测试 - # Judgement_AppData('SHO1','A','AppPack1',6144,'A-6144-Data','101.1.1.2:18010','201.21.1.2:18010',test_4) - # Judgement_TransData('ObjID','IDkey','TransPackID','TransSeq','TransTag','AppPackID','SentPort','RpctPort','SourceAppAddr','TargetAppAddr',TransPackedString) - # Judgement_IPData('ObjID','IDkey','IP_number','IPPackID','TransPackID','SourceIP','TargetIP','SourceAppAddr','TargetAppAddr',IPPackedString) - # Judgement_MACData('ObjID','IDkey','MAC_number','MacPackID','IPPackID','SentMAC','RcptMAC','SourceIP','TargetIP','SourceAppAddr','TargetAppAddr',MacPackedString) - ip_test = '127.0.0.1:18010' - ip_test2 = '196.168.2.1:18010' - ip_test_new = '196.168.1.1' - ip_test2_new = '196.168.3.1' - prot = 1395 - prot_1 = 65555 - test_mac = '00:1a:8c:10:ad:30' - test_mac1 = '10:00:00:90:fa:02:88:00' - # print(Judgement_Zz_IP(ip_test)) - # Judgement_AppData(ip_test,ip_test2) - # Judgement_TransData(Old_SourceAppAddr=ip_test,Old_TargetAppAddr=ip_test2,New_SourceAppAddr=ip_test_new,New_TargetAppAddr=ip_test2_new,SentPort=prot,RpctPort=prot_1) - Judgement_MACData(Old_SourceIP=ip_test_new,Old_TargetIP=ip_test2_new,New_SourceIP=ip_test2_new,New_TargetIP=ip_test_new,SentMAC=test_mac,RcptMAC=test_mac1) \ No newline at end of file diff --git a/Internet/pack_un_all.py b/Internet/pack_un_all.py deleted file mode 100644 index a7b84e8..0000000 --- a/Internet/pack_un_all.py +++ /dev/null @@ -1,189 +0,0 @@ -# 这个函数用来实现模拟解包过程 - - -# 创建类用来处理解包函数 -class Unpack_All(): - # 获取需要的数据 - def __init__(self,MacPacketString, IPPacketString, TransPacketString, AppPacketString): - self.MacPacketString = MacPacketString - self.IPPacketString = IPPacketString - self.TransPacketString = TransPacketString - self.AppPacketString = AppPacketString - - # 建立函数用来处理MacPacketString中的数据 - def unpack_mac_data(self): - # print(self.MacPacketString) - MacData_All = [] - # 将获取的数据分解成macdata中 - for item in self.MacPacketString: - ObjID = item['ObjID'] - ObjLable = item['ObjLable'] - MacPackId = item['MacPackId'] - IPPackID = item['IPPackID'] - sentmac = item['sentmac'] - rcptmac = item['rcptmac'] - sourceip = item['sourceip'] - targetip = item['targetip'] - sourceAppAddr = item['sourceAppAddr'] - targetAppAddr = item['targetAppAddr'] - MacData = { - 'ObjID': ObjID, - 'ObjLable': ObjLable, - 'MacPackId': MacPackId, - 'IPPackID': IPPackID, - 'sentmac': sentmac, - 'rcptmac': rcptmac, - 'sourceip': sourceip, - 'targetip': targetip, - 'sourceAppAddr': sourceAppAddr, - 'targetAppAddr': targetAppAddr, - 'MacPacketString': item - } - MacData_All.append(MacData) - return MacData_All - - # 建立函数用来处理IPPacket中的数据 - def unpack_ip_data(self): - IPData_All = [] - for item in self.IPPacketString: - ObjID = item['ObjID'] - ObjLable = item['ObjLable'] - IPPackID = item['IPPackID'] - TransPackID = item['TransPackID'] - sourceip = item['sourceip'] - targetip = item['targetip'] - sourceAppAddr = item['sourceAppAddr'] - targetAppAddr = item['targetAppAddr'] - IPData = { - 'ObjID': ObjID, - 'ObjLable': ObjLable, - 'IPPackID': IPPackID, - 'TransPackID': TransPackID, - 'sourceip': sourceip, - 'targetip': targetip, - 'sourceAppAddr': sourceAppAddr, - 'targetAppAddr': targetAppAddr, - 'IPPacketString': item - } - IPData_All.append(IPData) - return IPData_All - - - # 建立一个函数用来处理Trans的解包 - def unpack_trans_data(self): - TransData_All = [] - for item in self.TransPacketString: - ObjID = item['ObjID'] - ObjLable = item['ObjLable'] - TransPackID = item['TransPackID'] - TransSeq = item['TransSeq'] - TransTag = item['TransTag'] - AppPackId = item['AppPackId'] - sentport = item['sentport'] - rpctport = item['rpctport'] - sourceAppAddr = item['sourceAppAddr'] - targetAppAddr = item['targetAppAddr'] - TransData = { - 'ObjID': ObjID, - 'ObjLable': ObjLable, - 'TransPackID': TransPackID, - 'TransSeq': TransSeq, - 'TransTag': TransTag, - 'AppPackId': AppPackId, - 'sentport': sentport, - 'rpctport': rpctport, - 'sourceAppAddr': sourceAppAddr, - 'targetAppAddr': targetAppAddr, - 'TransPacketString': item - } - TransData_All.append(TransData) - return TransData_All - - # 建立一个函数用来处理App的解包 - def unpack_app_data(self): - ObjID = self.AppPacketString['ObjID'] - ObjLable = self.AppPacketString['ObjLable'] - AppPackId = self.AppPacketString['AppPackId'] - AppPackSize = self.AppPacketString['AppPackSize'] - AppPackTag = self.AppPacketString['AppPackTag'] - sourceAppAddr = self.AppPacketString['sourceAppAddr'] - targetAppAddr = self.AppPacketString['targetAppAddr'] - AppData = { - 'ObjID': ObjID, - 'ObjLable': ObjLable, - 'AppPackId': AppPackId, - 'AppPackSize': AppPackSize, - 'AppPackTag': AppPackTag, - 'sourceAppAddr': sourceAppAddr, - 'targetAppAddr': targetAppAddr, - 'AppPacketString': self.AppPacketString - } - # print(AppData) - return AppData - - -if __name__ == '__main__': - MacPacketString = [ - {'ObjID': 'SHO1', - 'ObjLable': 'A', - 'MAC_number': 1, - 'MacPackId': 'MacPackID1', - 'IPPackID': 'IPPack1', - 'sentmac': '00:1a:8c:10:ad:30', - 'rcptmac': '00:1a:8c:10:ad:20', - 'sourceip': '196.168.1.1', - 'targetip': '196.168.3.1', - 'sourceAppAddr': '196.168.1.1:63179', - 'targetAppAddr': '196.168.3.1:17242' - }] - - IPPacketString = [ - {'ObjID': 'SHO1', - 'ObjLable': 'A', - 'number': 1, - 'IPPackID': 'IPPack1', - 'TransPackID': 'TransPack1', - 'sourceip': '196.168.1.1', - 'targetip': '196.168.3.1', - 'sourceAppAddr': '196.168.1.1:37486', - 'targetAppAddr': '196.168.3.1:13191', - 'Mac_data': '00:1a:8c:10:ad:30'}] - - TransPacketString = [ - {'ObjID': 'SHO1', - 'ObjLable': 'A', - 'TransPackID': 'TransPack1', - 'TransSeq': '1', - 'TransTag': 'A1', - 'AppPackId': '1', - 'sentport': '41749', - 'rpctport': '20712', - 'sourceAppAddr': '196.168.1.1:41749', - 'targetAppAddr': '196.168.3.1:20712', - 'timestamp': '15:25:59', - 'Mac_data': '00:1a:8c:10:ad:30'}] - - AppPacketString = { - 'ObjID': 'SHO1', - 'ObjLable': 'A', - 'AppPackId': '1', - 'AppPackSize': 1, - 'AppPackTag': '1', - 'sourceAppAddr': '196.168.1.1:47325', - 'timeStamp': '15:22:28', - 'Mac_data': '00:1a:8c:10:ad:30', - 'targetAppAddr': '196.168.3.1:14035'} - - test_line = Unpack_All(MacPacketString,IPPacketString,TransPacketString,AppPacketString) - # test_line.unpack_mac_data() - test = test_line.unpack_app_data() - print(test) - print(test_line.unpack_trans_data()) - print(test_line.unpack_ip_data()) - print(test_line.unpack_mac_data()) - ''' - {'ObjID': 'SHO1', 'ObjLable': 'A', 'AppPackId': '1', 'AppPackSize': 1, 'AppPackTag': '1', 'sourceAppAddr': '196.168.1.1:47325', 'targetAppAddr': '196.168.3.1:14035', 'AppPacketString': {'ObjID': 'SHO1', 'ObjLable': 'A', 'AppPackId': '1', 'AppPackSize': 1, 'AppPackTag': '1', 'sourceAppAddr': '196.168.1.1:47325', 'timeStamp': '15:22:28', 'Mac_data': '00:1a:8c:10:ad:30', 'targetAppAddr': '196.168.3.1:14035'}} - [{'ObjID': 'SHO1', 'ObjLable': 'A', 'TransPackID': 'TransPack1', 'TransSeq': '1', 'TransTag': 'A1', 'AppPackId': '1', 'sentport': '41749', 'rpctport': '20712', 'sourceAppAddr': '196.168.1.1:41749', 'targetAppAddr': '196.168.3.1:20712', 'TransPacketString': {'ObjID': 'SHO1', 'ObjLable': 'A', 'TransPackID': 'TransPack1', 'TransSeq': '1', 'TransTag': 'A1', 'AppPackId': '1', 'sentport': '41749', 'rpctport': '20712', 'sourceAppAddr': '196.168.1.1:41749', 'targetAppAddr': '196.168.3.1:20712', 'timestamp': '15:25:59', 'Mac_data': '00:1a:8c:10:ad:30'}}] - [{'ObjID': 'SHO1', 'ObjLable': 'A', 'IPPackID': 'IPPack1', 'TransPackID': 'TransPack1', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:37486', 'targetAppAddr': '196.168.3.1:13191', 'IPPacketString': {'ObjID': 'SHO1', 'ObjLable': 'A', 'number': 1, 'IPPackID': 'IPPack1', 'TransPackID': 'TransPack1', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:37486', 'targetAppAddr': '196.168.3.1:13191', 'Mac_data': '00:1a:8c:10:ad:30'}}] - [{'ObjID': 'SHO1', 'ObjLable': 'A', 'MacPackId': 'MacPackID1', 'IPPackID': 'IPPack1', 'sentmac': '00:1a:8c:10:ad:30', 'rcptmac': '00:1a:8c:10:ad:20', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:63179', 'targetAppAddr': '196.168.3.1:17242', 'MacPacketString': {'ObjID': 'SHO1', 'ObjLable': 'A', 'MAC_number': 1, 'MacPackId': 'MacPackID1', 'IPPackID': 'IPPack1', 'sentmac': '00:1a:8c:10:ad:30', 'rcptmac': '00:1a:8c:10:ad:20', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:63179', 'targetAppAddr': '196.168.3.1:17242'}}] - ''' diff --git a/Internet/packet_all.py b/Internet/packet_all.py deleted file mode 100644 index 7bf448d..0000000 --- a/Internet/packet_all.py +++ /dev/null @@ -1,285 +0,0 @@ -# 封包处理函数 -import random -import datetime - - -# 建立一个类将所有的函数放入其中 -# 将所有的主机路由器详细数据存入一个字典列表中? -# 将主机路由器的ObjID存入一个数组中通过循环来判断下一级的RctpMAC. -# MACID = ['MacA','MacR1', 'MacR2', 'MacR4', 'MacR5', 'MacD'] -# 不能将所有的函数整合成一个类 - -# 建立一个全局变量IP_number -IP_number = 0 -MAC_number = 0 - - -class AllData(): - ''' - MACID: 这个数据将从数据库simhost获取所有的数据并将每条数据中以先后组成一个mac地址数组 - ShowData: 这个数据是将数据库simhost中的所有数据组成一个字典数组从里面 - AppPackTag: 这个数据是需要用户点击前端页面的封包模拟里面输入的数据 - ''' - def __init__(self, MACID, ShowData, AppPackTag_Data): - self.MACID = MACID - self.ShowData = ShowData - self.AppPackTag_Data = AppPackTag_Data - - - def AppData(self): - MACID = self.MACID - ShowData = self.ShowData - AppPackTag_Data = self.AppPackTag_Data - # 拿到前台传过来的数据并处理 - # 将收集的数据进行处理 - ''' - test: - 这里添加一个对字典列表ShowData的for循环 - 然后对字典列表里的['ObjID']进行判断,判断是否为开始主机 - ''' - # print(len(ShowData)) - AppPackedString = {} - for i in range(0,len(ShowData)): - # print(i) - if i == 0 and ShowData[i]['ObjType'] == 1: - ObjID = ShowData[i]['ObjID'] - ObjLable = ShowData[i]['ObjLable'] - AppPackId = AppPackTag_Data['AppPackID'] - AppPackSize = AppPackTag_Data['AppPackSize'] - AppPackTag = AppPackTag_Data['AppPackTag'] - sentport = random.randint(2048, 65535) - sourceAppAddr = ShowData[i]['IPAddr'] + ':' + str(sentport) - timeStamp = datetime.datetime.now().strftime('%H:%M:%S') - Mac_data = ShowData[i]['MACAddr'] - # 对字典进行添加 - AppPackedString['ObjID'] = ObjID - AppPackedString['ObjLable'] = ObjLable - AppPackedString['AppPackId'] = AppPackId - AppPackedString['AppPackSize'] = AppPackSize - AppPackedString['AppPackTag'] = AppPackTag - AppPackedString['sourceAppAddr'] = sourceAppAddr - AppPackedString['timeStamp'] = timeStamp - AppPackedString['Mac_data'] = Mac_data - if i == (len(ShowData)-1): - rpctport = random.randint(2048, 65535) - targetAppAddr = ShowData[i]['IPAddr'] + ':' + str(rpctport) - AppPackedString['targetAppAddr'] = targetAppAddr - return AppPackedString - - def TransData(self,AppPackedString): - ''' - 这里需要处理获取的信息 - ''' - # print(AppPackedString) - AppPackSize = AppPackedString['AppPackSize'] - page_true = Spilt_AppData(AppPackSize) - # 定义一个空列表存放所有TransPackedString - TransPackedString_All = [] - # 将数据包分为page_true个传输层数据包 - for i in range(0, page_true): - num = i + 1 - # 这里需要建立一个将Trans数据封包函数 - TransPackedString = insert_TransData(num, AppPackedString) - TransPackedString_All.append(TransPackedString) - # print(TransPackedString_All) - return TransPackedString_All - - - - - - # 这里建立一个IP层封包数据函数 - def IPData(self,TransPackedString): - # 将获取的字典数组进行拆分 - IPPackedString_All = [] - for item in TransPackedString: - global IP_number - IP_number = IP_number + 1 # IP_number为记录数据经过函数IPData的次数 - ObjID = item['ObjID'] - ObjLable = item['ObjLable'] - IPPackID = 'IPPack' + str(IP_number) - TransPackID = item['TransPackID'] - sourceip_true = item['sourceAppAddr'].split(':') - sourceip = sourceip_true[0] - targetip_true = item['targetAppAddr'].split(':') - targetip = targetip_true[0] - sourceAppAddr = item['sourceAppAddr'] - targetAppAddr = item['targetAppAddr'] - Mac_data = item['Mac_data'] - IPPackedString = { - 'ObjID': ObjID, - 'ObjLable': ObjLable, - 'number': IP_number, - 'IPPackID': IPPackID, - 'TransPackID': TransPackID, - 'sourceip': sourceip, - 'targetip': targetip, - 'sourceAppAddr': sourceAppAddr, - 'targetAppAddr': targetAppAddr, - 'Mac_data': Mac_data - } - IPPackedString_All.append(IPPackedString) - return IPPackedString_All - - - '''{'ObjID': 'SHO1', 'ObjLable': 'A', 'AppPackId': 'AppPack1', 'AppPackSize': 6144, 'AppPackTag': 'A-6144-Data', 'sourceAppAddr': '196.168.1.1:35239', 'timeStamp': '13:03:25', 'Mac_data': '00:1a:8c:10:ad:30', 'targetAppAddr': '196.168.3.1:45771'} - [{'ObjID': 'SHO1', 'ObjLable': 'A', 'TransPackID': 'TransPack1', 'TransSeq': '1', 'TransTag': 'A1', 'AppPackId': 'AppPack1', 'sentport': '35239', 'rpctport': '45771', 'sourceAppAddr': '196.168.1.1:35239', 'targetAppAddr': '196.168.3.1:45771', 'timestamp': '13:03:25', 'Mac_data': '00:1a:8c:10:ad:30'}, {'ObjID': 'SHO1', 'ObjLable': 'A', 'TransPackID': 'TransPack2', 'TransSeq': '2', 'TransTag': 'A2', 'AppPackId': 'AppPack1', 'sentport': '35239', 'rpctport': '45771', 'sourceAppAddr': '196.168.1.1:35239', 'targetAppAddr': '196.168.3.1:45771', 'timestamp': '13:03:25', 'Mac_data': '00:1a:8c:10:ad:30'}, {'ObjID': 'SHO1', 'ObjLable': 'A', 'TransPackID': 'TransPack3', 'TransSeq': '3', 'TransTag': 'A3', 'AppPackId': 'AppPack1', 'sentport': '35239', 'rpctport': '45771', 'sourceAppAddr': '196.168.1.1:35239', 'targetAppAddr': '196.168.3.1:45771', 'timestamp': '13:03:25', 'Mac_data': '00:1a:8c:10:ad:30'}] - [{'ObjID': 'SHO1', 'ObjLable': 'A', 'number': 1, 'IPPackID': 'IPPack1', 'TransPackID': 'TransPack1', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:35239', 'targetAppAddr': '196.168.3.1:45771', 'Mac_data': '00:1a:8c:10:ad:30'}, {'ObjID': 'SHO1', 'ObjLable': 'A', 'number': 2, 'IPPackID': 'IPPack2', 'TransPackID': 'TransPack2', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:35239', 'targetAppAddr': '196.168.3.1:45771', 'Mac_data': '00:1a:8c:10:ad:30'}, {'ObjID': 'SHO1', 'ObjLable': 'A', 'number': 3, 'IPPackID': 'IPPack3', 'TransPackID': 'TransPack3', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:35239', 'targetAppAddr': '196.168.3.1:45771', 'Mac_data': '00:1a:8c:10:ad:30'}] - [{'ObjID': 'SHO1', 'ObjLable': 'A', 'MAC_number': 1, 'MacPackId': 'MacPackID1', 'IPPackID': 'IPPack1', 'sentmac': '00:1a:8c:10:ad:30', 'rcptmac': '00:1a:8c:10:ad:30', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:35239', 'targetAppAddr': '196.168.3.1:45771'}, {'ObjID': 'SHO1', 'ObjLable': 'A', 'MAC_number': 2, 'MacPackId': 'MacPackID2', 'IPPackID': 'IPPack2', 'sentmac': '00:1a:8c:10:ad:30', 'rcptmac': '00:1a:8c:10:ad:30', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:35239', 'targetAppAddr': '196.168.3.1:45771'}, {'ObjID': 'SHO1', 'ObjLable': 'A', 'MAC_number': 3, 'MacPackId': 'MacPackID3', 'IPPackID': 'IPPack3', 'sentmac': '00:1a:8c:10:ad:30', 'rcptmac': '00:1a:8c:10:ad:30', 'sourceip': '196.168.1.1', 'targetip': '196.168.3.1', 'sourceAppAddr': '196.168.1.1:35239', 'targetAppAddr': '196.168.3.1:45771'}] - ''' - - # 这里建立一个MAC层封包数据函数 - # def MACData(self,IPPackedString,MACID): - def MACData(self, IPPackedString): - # 建立一个空数组用来存放所有的数据 - MacPackedString_All = [] - for item in IPPackedString: - global MAC_number - rcptmac = '' - ObjID = item['ObjID'] - ObjLable = item['ObjLable'] - MAC_number = MAC_number + 1 - MacPackId = 'MacPackID' + str(MAC_number) - IPPackID = item['IPPackID'] - sentmac = item['Mac_data'] - # 循环MACID来获取下一级rcptmac - for i in range(len(self.MACID)-1): - if sentmac == self.MACID[i]: - rcptmac = self.MACID[i+1] - - - - sourceip = item['sourceip'] - targetip = item['targetip'] - sourceAppAddr = item['sourceAppAddr'] - targetAppAddr = item['targetAppAddr'] - MacPackedString = { - 'ObjID': ObjID, - 'ObjLable': ObjLable, - 'MAC_number': MAC_number, - 'MacPackId': MacPackId, - 'IPPackID': IPPackID, - 'sentmac': sentmac, - 'rcptmac': rcptmac, - 'sourceip': sourceip, - 'targetip': targetip, - 'sourceAppAddr': sourceAppAddr, - 'targetAppAddr': targetAppAddr - } - MacPackedString_All.append(MacPackedString) - return MacPackedString_All - - - -# 建立一个使应用层数据包进行拆分的函数 -def Spilt_AppData(AppPackSize): - # 将获取的数据包大小进行拆分 - page = int(AppPackSize) / 2048 - # 定义一个初始值 - page_true = 0 - if page < 1 and page != 0: - page_true = int(page) + 1 - elif page % 1 == 0: - page_true = int(page) - elif page % 1 > 0: - page_true = int(page) + 1 - return page_true - - -# 建立一个进行TransData数据封包函数 -def insert_TransData(num, AppPackedString): - TransPackID_Riget = num - ObjID = AppPackedString['ObjID'] - ObjLable = AppPackedString['ObjLable'] - TransPackID = 'TransPack'+str(TransPackID_Riget) - TransSeq = str(TransPackID_Riget) - TransTag = AppPackedString['ObjLable']+str(TransPackID_Riget) - AppPackId = AppPackedString['AppPackId'] - sentport_one = AppPackedString['sourceAppAddr'].split(':') - # print(sentport_one) - sentport = sentport_one[1] - # print(sentport) - rpctport_one = AppPackedString['targetAppAddr'].split(':') - rpctport = rpctport_one[1] - sourceAppAddr = AppPackedString['sourceAppAddr'] - targetAppAddr = AppPackedString['targetAppAddr'] - timestamp = datetime.datetime.now().strftime('%H:%M:%S') - Mac_data = AppPackedString['Mac_data'] - TransPackedString = { - 'ObjID': ObjID, - 'ObjLable': ObjLable, - 'TransPackID': TransPackID, - 'TransSeq': TransSeq, - 'TransTag': TransTag, - 'AppPackId': AppPackId, - 'sentport': sentport, - 'rpctport': rpctport, - 'sourceAppAddr': sourceAppAddr, - 'targetAppAddr': targetAppAddr, - 'timestamp': timestamp, - 'Mac_data': Mac_data - } - # print(TransPackedString) - return TransPackedString - - - - -if __name__ == '__main__': - # 建立一个列表来存放ObjID里的MAC地址 , test - MACID = ['00:1a:8c:10:ad:30', '00:1a:8c:10:ad:30'] - # MACID = { - # 'A': 'MacA', - # 'R1': 'MacR1', - # 'R2': 'MacR2', - # 'R4': 'MacR4', - # 'R5': 'MacR5', - # 'D': 'MacD' - # } - # 定义一个数据用来模拟前台发送过来的数据:字典类型最好, test - # 这个位置需要的使一个字典列表, 至少需要两个以上的字典 - ''' - ShowData为字典数组,进行到第三步需要将数据修改进行替换, - 此数据需要前端传入后端以json格式 - ''' - # port 好需要通过random来实现 - ShowData = [ - { - 'id': 1, - 'ObjID': 'SHO1', - 'ObjType':1, - 'ObjLable': 'A', - 'IPAddr': '196.168.1.1', # 起始IP地址 - 'ObjX': 210, - 'ObjY': 130, - 'MACAddr': '00:1a:8c:10:ad:30' # test用mac地址 - }, - { - 'id': 1, - 'ObjID': 'SRO1', - 'ObjType': 2, - 'ObjLable': 'R1', - 'IPAddr': '196.168.3.1', # 最终IP地址 - 'ObjX': 210, - 'ObjY':130, - 'MACAddr': '00:1a:8c:10:ad:30' - } - ] - AppPackTag_Data = { - 'AppPackID': 'AppPack1', - 'AppPackSize': 6144, - 'AppPackTag': 'A-6144-Data' - } - # 需要定义一个最终IP地址路由节点用来进行测试 test - # 测试主程序 - AllData = AllData(MACID, ShowData, AppPackTag_Data) - test = AllData.AppData() - print(test) - test_1 = AllData.TransData(test) - print(test_1) - test_2 = AllData.IPData(test_1) - print(test_2) - print(AllData.MACData(test_2)) - ''' - 封包函数数据以全部可以获取,现在进行主机和路由器之间的交互 - ''' \ No newline at end of file diff --git a/LICENSE b/LICENSE deleted file mode 100644 index baee873..0000000 --- a/LICENSE +++ /dev/null @@ -1,170 +0,0 @@ -Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International - - Creative Commons Corporation (“Creative Commons”) is not a law firm and does not provide legal services or legal advice. Distribution of Creative Commons public licenses does not create a lawyer-client or other relationship. Creative Commons makes its licenses and related information available on an “as-is” basis. Creative Commons gives no warranties regarding its licenses, any material licensed under their terms and conditions, or any related information. Creative Commons disclaims all liability for damages resulting from their use to the fullest extent possible. - -Using Creative Commons Public Licenses - -Creative Commons public licenses provide a standard set of terms and conditions that creators and other rights holders may use to share original works of authorship and other material subject to copyright and certain other rights specified in the public license below. The following considerations are for informational purposes only, are not exhaustive, and do not form part of our licenses. - -Considerations for licensors: Our public licenses are intended for use by those authorized to give the public permission to use material in ways otherwise restricted by copyright and certain other rights. Our licenses are irrevocable. Licensors should read and understand the terms and conditions of the license they choose before applying it. Licensors should also secure all rights necessary before applying our licenses so that the public can reuse the material as expected. Licensors should clearly mark any material not subject to the license. This includes other CC-licensed material, or material used under an exception or limitation to copyright. More considerations for licensors. - -Considerations for the public: By using one of our public licenses, a licensor grants the public permission to use the licensed material under specified terms and conditions. If the licensor’s permission is not necessary for any reason–for example, because of any applicable exception or limitation to copyright–then that use is not regulated by the license. Our licenses grant only permissions under copyright and certain other rights that a licensor has authority to grant. Use of the licensed material may still be restricted for other reasons, including because others have copyright or other rights in the material. A licensor may make special requests, such as asking that all changes be marked or described. Although not required by our licenses, you are encouraged to respect those requests where reasonable. More considerations for the public. - -Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License - -By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions. - -Section 1 – Definitions. - - a. Adapted Material means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image. - - b. Adapter's License means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License. - - c. BY-NC-SA Compatible License means a license listed at creativecommons.org/compatiblelicenses, approved by Creative Commons as essentially the equivalent of this Public License. - - d. Copyright and Similar Rights means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. - - e. Effective Technological Measures means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements. - - f. Exceptions and Limitations means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material. - - g. License Elements means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution, NonCommercial, and ShareAlike. - - h. Licensed Material means the artistic or literary work, database, or other material to which the Licensor applied this Public License. - - i. Licensed Rights means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license. - - j. Licensor means the individual(s) or entity(ies) granting rights under this Public License. - - k. NonCommercial means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of this Public License, the exchange of the Licensed Material for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange. - - l. Share means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them. - - m. Sui Generis Database Rights means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world. - - n. You means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning. - -Section 2 – Scope. - - a. License grant. - - 1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to: - - A. reproduce and Share the Licensed Material, in whole or in part, for NonCommercial purposes only; and - - B. produce, reproduce, and Share Adapted Material for NonCommercial purposes only. - - 2. Exceptions and Limitations. For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions. - - 3. Term. The term of this Public License is specified in Section 6(a). - - 4. Media and formats; technical modifications allowed. The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material. - - 5. Downstream recipients. - - A. Offer from the Licensor – Licensed Material. Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License. - - B. Additional offer from the Licensor – Adapted Material. Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapter’s License You apply. - - C. No downstream restrictions. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material. - - 6. No endorsement. Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i). - - b. Other rights. - - 1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise. - - 2. Patent and trademark rights are not licensed under this Public License. - - 3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties, including when the Licensed Material is used other than for NonCommercial purposes. - -Section 3 – License Conditions. - -Your exercise of the Licensed Rights is expressly made subject to the following conditions. - - a. Attribution. - - 1. If You Share the Licensed Material (including in modified form), You must: - - A. retain the following if it is supplied by the Licensor with the Licensed Material: - - i. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated); - - ii. a copyright notice; - - iii. a notice that refers to this Public License; - - iv. a notice that refers to the disclaimer of warranties; - - v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable; - - B. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and - - C. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License. - - 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information. - - 3. If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable. - - b. ShareAlike.In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply. - - 1. The Adapter’s License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-NC-SA Compatible License. - - 2. You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material. - - 3. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply. - -Section 4 – Sui Generis Database Rights. - -Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material: - - a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database for NonCommercial purposes only; - - b. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and - - c. You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database. -For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights. - -Section 5 – Disclaimer of Warranties and Limitation of Liability. - - a. Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You. - - b. To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You. - - c. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability. - -Section 6 – Term and Termination. - - a. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically. - - b. Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates: - - 1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or - - 2. upon express reinstatement by the Licensor. - - For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License. - - c. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License. - - d. Sections 1, 5, 6, 7, and 8 survive termination of this Public License. - -Section 7 – Other Terms and Conditions. - - a. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed. - - b. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License. - -Section 8 – Interpretation. - - a. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License. - - b. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions. - - c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor. - - d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority. - -Creative Commons is not a party to its public licenses. Notwithstanding, Creative Commons may elect to apply one of its public licenses to material it publishes and in those instances will be considered the “Licensor.” Except for the limited purpose of indicating that material is shared under a Creative Commons public license or as otherwise permitted by the Creative Commons policies published at creativecommons.org/policies, Creative Commons does not authorize the use of the trademark “Creative Commons” or any other trademark or logo of Creative Commons without its prior written consent including, without limitation, in connection with any unauthorized modifications to any of its public licenses or any other arrangements, understandings, or agreements concerning use of licensed material. For the avoidance of doubt, this paragraph does not form part of the public licenses. - -Creative Commons may be contacted at creativecommons.org. diff --git a/README.md b/README.md index b73dc94..775b919 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,195 @@ -# NetWork +# 项目编程基本原则 + +## 文档及代码编辑的注意事项 + +### 进度汇报 +**查文档和编程任务是否符合。** + +首先自查一遍文档,每个编程任务是否符合文档上编程任务的要求---请在进度表中标示【自查:符合 或 不符合】,是按编程任务,不是按X1,X2... + +**查命名问题,函数框架,函数名称,和变量名规范** + +(2)按我们前两天讨论的,主函数框架、函数关系调用图、函数名称与变量名称规范化等方面回复【已优化完成,未优化完成】 + + +### 主函数框架示例: + +```python +# 需要导入的库 + +from tkinter import scrolledtext # 下拉框 +from tkinter import END # 下拉框自动下拉到最后一行 +from pymouse import PyMouse # 模拟鼠标自动点击 +import tkinter as tk # UI界面 +import threading # 多线程任务 +import win32gui # 获取扫雷窗口信息 +import random # 随机数 +import time # 应用sleep函数睡眠 + +"""按照上宽下窄的方式排列引用""" + + +""" +所有的系统库或第三方库,一次性引出,避免因为确少库而导致运行不了。 +如果是自写的模块,可以在函数前引出 +""" + +#方式1--在文件中定义全局变量,加载全局变量定义文件(如果全局变量实在太多) +from setting import * # (setting为自定义程序存放的是全局变量)导入全局变量 +# setting中定义了哪些全局变量及其定义 + + +#方式2--直接定义全局常量和变量(一般多) +全局常量 +GLOBAL ... ... # 常量用大写,大写字母下划线链接 +全局变量 +Init_Matrix ... ... # 全局变量首字母大写 + +"""按照上宽下窄的方式排列引用""" + + + +注释标准: +就近对齐规则 +print("数据集中图片的个数:", len(data_list)) # 打印图片数据集中图片的个数 +print("第一个图片数组的形状:", data_list[0].shape) # 打印第一个图片数组的形状 +print("第一个图片数组的内容:\n", data_list[0]) # 打印第一个图片数组的内容 +return data_list # 返回加载成功的图像数据列表 + + +########### +'此处说明是放函数定义的地方' +########### + + +# 主控函数, 将函数定义拷贝到main函数上面,将函数调用放在main函数 +if __name__ == '__main__': + pass + + ########### + '此处说明是函数调用的地方' + ########### + +""" +要求主函数框架单独运行不报错 +""" +``` + +```python +# 文档贴函数提示说明 如下图所示 +########################################################################### + +# 以func1()[编程章节]==>func2()[编程章节]的方式说明调用当前函数要用的前述函数。 +set_random_state()[5.1]==>create_boards()[5.4]==>show_window()[5.4] +``` +![img_2.png](img_2.png) + +### 根据不同情况不同的贴程序方法 + +```python +# 情况一 +##### 第一次出现init初始化方法需要写全 ##### +##### 在类里面增添方法,需要将类名写出,再写方法 ##### +X2: +class MineSweeper: # 建立扫雷类 + def __init__(self): # 第一次出现init初始化方法需要写全, + def f2(): + pass + +# 情况二 +##### 如果函数实在太多,需要标出函数调用关系图,同时将贴合编程要求的程序给出 ##### +X3: # f1(), f2(), f3(), f4(), ..., f10() 本段仅给出f4(), 完整的在[位置](可以在文档末尾建立附录)。 +Class Class_some + ... + def f4(): 体现核心功能的函数给出。 + pass + +# 情况三 +##### 如果需要在后续编程章节中扩充原有的功能,可以扩写函数,##### +##### 而且不要出现已经出现过的代码,已经出现过的代码用...代指 ##### +X4: 【友好阅读性原则;最小重复代码原则;递增式代码表达原则;界面元素处理与业务逻辑完全分开原则】 +class MineSweeper: # 扩展扫雷类--在X2基础上进一步定义 + # _init_(self), f2(), 一并处理 + def _init_(self): #在原函数基础上增加/调整 + ... + self.par = "example" + def f3(): + ... + def f4(): + ... + +每个函数不超一页,原则上就是半页。 + +``` + +## 项目编程的普遍要求 + +### GUI界面和数据结构分开 + +借用前后端分离的架构模式。 + +GUI界面负责处理用户界面的展示和交互,数据结构部分负责处理业务逻辑和数据处理。 + +将GUI界面负和数据结构分为两个独立的部分,各自负责不同的功能和任务。GUI界面通过接口函数与更新和加载数据结构。 + +### 代码命名规则: + +在Python中,有一些命名规则和约定被广泛遵循,以提高代码的可读性和一致性。 + +全局变量:首字母大写单词之间用下划线分隔。 + +变量和函数名:使用小写字母,单词之间使用下划线(snake_case)进行分隔。例如:my_variable, calculate_result。 + +常量:使用全大写字母,单词之间使用下划线进行分隔。例如:MAX_VALUE, PI。 + +类名:使用驼峰命名法(CamelCase),即首字母大写,不使用下划线。例如:MyClass, Calculator。 + +模块名:使用小写字母,单词之间使用下划线进行分隔。例如:my_module, utils。 + +- 首先是做好程序的优化,即做好程序函数的封装。 每个函数的函数名尽可能符合文档的编程任务及其要求--要让读者看到函数名就能想到编程任务,这样会更易于理解。 + +- 函数调用关系图的理解是正确的,就是要有一张全局关系图。 + +- 正确理解X1、X2、X3、X4和X5。通常,X1仅是围绕数据结构赋值,和界面或者界面元素坐标无关。X2是依据数据结构中的数据做输出,通常是仅输出。X3通常可以定义函数,或者定义类及其中的函数。X4是在X2基础上做界面交互元素,同时将界面交互元素相关的事件/消息绑定X3或X5的函数。X5也是一些函数。这样界面逻辑和业务逻辑是完全分离的。 + +- 注意:自前向后,编程是越来越复杂,但不要用后面的编程直接替代前面的编程任务中的程序,前面是简单的后面是复杂的。例如“Select Sno from Student”,这是前面的编程,Select :attr1 from :table1",尽管将attr1赋值成Sno,将table1赋值成Student,也能实现前面的语句,但不可用后面的替换前面的。 + + +### 函数调用关系图: + +函数调用图(Function Call Graph)是用于描述程序中函数之间调用关系的图形表示。它展示了函数之间的依赖关系和调用流程,帮助我们理解程序的执行流程和函数之间的交互 + +在函数调用图中,函数被表示为节点,函数之间的调用关系被表示为边。每个函数调用都会生成一个新的节点和一条连接调用者和被调用者的边。函数调用图可以是有向图或无向图,具体取决于函数调用的方向性。 + +调用图可以用软件生成。 + +![img.png](img.png) + +### 项目文件应包含(后续规范) +项目应该包含以下基本文件: + +1. README.md:项目的说明文档,包含项目的介绍、使用方法、安装指南、贡献指南等重要信息。README.md通常是其他开发者了解和使用项目的入口。 + +2. LICENSE:项目的开源许可证,明确了项目的使用条件和权利限制。选择适合项目的开源许可证对于保护项目的权益和推动开源合作非常重要。 + +3. .gitignore:Git版本控制系统的忽略文件配置,用于指定哪些文件或目录应该被忽略,不纳入版本控制。通常包括一些编译生成的文件、临时文件、敏感信息等。 + +4. requirements.txt:项目的依赖项清单,列出了项目所需的外部库、框架和工具的版本信息。这样其他开发者可以方便地安装相同的依赖项,确保项目的可重复性和一致性。 + +5. setup.py 或者 setup.cfg:用于打包和发布项目的配置文件。可以定义项目的元数据、依赖关系、安装过程等,以便其他人能够方便地安装和使用项目。 + +6. docs 目录:包含项目的文档,例如用户手册、API文档、开发者指南等。良好的文档对于其他开发者和用户理解和使用项目非常重要。 + +7. tests 目录:包含项目的测试代码和测试数据,用于验证项目的正确性和稳定性。包括单元测试、集成测试等,帮助开发者确保项目的质量和可靠性。 + +8. src 或者 lib 目录:包含项目的源代码文件。根据项目的规模和结构,可以进一步组织成子目录或包,方便代码的组织和维护。 + +除了上述基本文件,根据项目的特点和需求,还可以包含其他文件,如配置文件、示例代码、演示视频等。重要的是根据项目的具体情况进行文件的组织和描述,确保项目的可理解性、可维护性和可扩展性。 + +```bash +# 将输出重定向到文件: +pip freeze > requirements.txt +# 老师一键安装项目所有依赖,一般像Pycharm会自动识别这个文件,按提示安装即可 +pip install -r requirements.txt +``` diff --git a/Internet/internet.db b/database.db similarity index 63% rename from Internet/internet.db rename to database.db index d1e1b74..1347690 100644 Binary files a/Internet/internet.db and b/database.db differ diff --git a/datas/images/packet.png b/datas/images/packet.png new file mode 100644 index 0000000..50a8ef0 Binary files /dev/null and b/datas/images/packet.png differ diff --git a/datas/images/主机--暗色.svg b/datas/images/主机--暗色.svg new file mode 100644 index 0000000..c5250f3 --- /dev/null +++ b/datas/images/主机--暗色.svg @@ -0,0 +1,9 @@ + + + 主机--暗色 + + + + + + \ No newline at end of file diff --git a/datas/images/主机--暗色@1x.png b/datas/images/主机--暗色@1x.png new file mode 100644 index 0000000..9e0b53a Binary files /dev/null and b/datas/images/主机--暗色@1x.png differ diff --git a/datas/images/主机--暗色@2x.png b/datas/images/主机--暗色@2x.png new file mode 100644 index 0000000..198bd06 Binary files /dev/null and b/datas/images/主机--暗色@2x.png differ diff --git a/datas/images/主机.png b/datas/images/主机.png new file mode 100644 index 0000000..58189e3 Binary files /dev/null and b/datas/images/主机.png differ diff --git a/datas/images/主机.svg b/datas/images/主机.svg new file mode 100644 index 0000000..dd17a26 --- /dev/null +++ b/datas/images/主机.svg @@ -0,0 +1,55 @@ + + + 主机 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/datas/images/主机@1x.png b/datas/images/主机@1x.png new file mode 100644 index 0000000..2f0d607 Binary files /dev/null and b/datas/images/主机@1x.png differ diff --git a/datas/images/主机@2x.png b/datas/images/主机@2x.png new file mode 100644 index 0000000..f7f2600 Binary files /dev/null and b/datas/images/主机@2x.png differ diff --git a/datas/images/主机_tm.png b/datas/images/主机_tm.png new file mode 100644 index 0000000..a2e634a Binary files /dev/null and b/datas/images/主机_tm.png differ diff --git a/datas/images/交换机--暗色.svg b/datas/images/交换机--暗色.svg new file mode 100644 index 0000000..d0b01d1 --- /dev/null +++ b/datas/images/交换机--暗色.svg @@ -0,0 +1,9 @@ + + + 交换机--暗色 + + + + + + \ No newline at end of file diff --git a/datas/images/交换机--暗色@1x.png b/datas/images/交换机--暗色@1x.png new file mode 100644 index 0000000..664084a Binary files /dev/null and b/datas/images/交换机--暗色@1x.png differ diff --git a/datas/images/交换机--暗色@2x.png b/datas/images/交换机--暗色@2x.png new file mode 100644 index 0000000..b3c782d Binary files /dev/null and b/datas/images/交换机--暗色@2x.png differ diff --git a/datas/images/交换机.png b/datas/images/交换机.png new file mode 100644 index 0000000..1467d82 Binary files /dev/null and b/datas/images/交换机.png differ diff --git a/datas/images/交换机.svg b/datas/images/交换机.svg new file mode 100644 index 0000000..8c4551e --- /dev/null +++ b/datas/images/交换机.svg @@ -0,0 +1,391 @@ + + + 交换机 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/datas/images/交换机@1x.png b/datas/images/交换机@1x.png new file mode 100644 index 0000000..32115d7 Binary files /dev/null and b/datas/images/交换机@1x.png differ diff --git a/datas/images/交换机@2x.png b/datas/images/交换机@2x.png new file mode 100644 index 0000000..22799bc Binary files /dev/null and b/datas/images/交换机@2x.png differ diff --git a/datas/images/交换机_tm.png b/datas/images/交换机_tm.png new file mode 100644 index 0000000..e2d78d0 Binary files /dev/null and b/datas/images/交换机_tm.png differ diff --git a/datas/images/右上框.png b/datas/images/右上框.png new file mode 100644 index 0000000..15c561e Binary files /dev/null and b/datas/images/右上框.png differ diff --git a/datas/images/右上框@2x.png b/datas/images/右上框@2x.png new file mode 100644 index 0000000..d9e9ede Binary files /dev/null and b/datas/images/右上框@2x.png differ diff --git a/datas/images/右下框.png b/datas/images/右下框.png new file mode 100644 index 0000000..f29a074 Binary files /dev/null and b/datas/images/右下框.png differ diff --git a/datas/images/右下框@2x.png b/datas/images/右下框@2x.png new file mode 100644 index 0000000..38fdbf6 Binary files /dev/null and b/datas/images/右下框@2x.png differ diff --git a/datas/images/日志消息展示框.png b/datas/images/日志消息展示框.png new file mode 100644 index 0000000..e280753 Binary files /dev/null and b/datas/images/日志消息展示框.png differ diff --git a/datas/images/日志消息展示框@2x.png b/datas/images/日志消息展示框@2x.png new file mode 100644 index 0000000..6999741 Binary files /dev/null and b/datas/images/日志消息展示框@2x.png differ diff --git a/datas/images/背景.png b/datas/images/背景.png new file mode 100644 index 0000000..827527b Binary files /dev/null and b/datas/images/背景.png differ diff --git a/datas/images/背景@2x.png b/datas/images/背景@2x.png new file mode 100644 index 0000000..615f60e Binary files /dev/null and b/datas/images/背景@2x.png differ diff --git a/datas/images/路由器--暗色.svg b/datas/images/路由器--暗色.svg new file mode 100644 index 0000000..d40a4e1 --- /dev/null +++ b/datas/images/路由器--暗色.svg @@ -0,0 +1,9 @@ + + + 路由器--暗色 + + + + + + \ No newline at end of file diff --git a/datas/images/路由器--暗色@1x.png b/datas/images/路由器--暗色@1x.png new file mode 100644 index 0000000..f836c2f Binary files /dev/null and b/datas/images/路由器--暗色@1x.png differ diff --git a/datas/images/路由器--暗色@2x.png b/datas/images/路由器--暗色@2x.png new file mode 100644 index 0000000..cbbb492 Binary files /dev/null and b/datas/images/路由器--暗色@2x.png differ diff --git a/datas/images/路由器.png b/datas/images/路由器.png new file mode 100644 index 0000000..cb99d04 Binary files /dev/null and b/datas/images/路由器.png differ diff --git a/datas/images/路由器.svg b/datas/images/路由器.svg new file mode 100644 index 0000000..ee28574 --- /dev/null +++ b/datas/images/路由器.svg @@ -0,0 +1,68 @@ + + + 路由器 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/datas/images/路由器@1x.png b/datas/images/路由器@1x.png new file mode 100644 index 0000000..d6959d7 Binary files /dev/null and b/datas/images/路由器@1x.png differ diff --git a/datas/images/路由器@2x.png b/datas/images/路由器@2x.png new file mode 100644 index 0000000..ffb4ee4 Binary files /dev/null and b/datas/images/路由器@2x.png differ diff --git a/datas/images/路由器_tm.png b/datas/images/路由器_tm.png new file mode 100644 index 0000000..8c17004 Binary files /dev/null and b/datas/images/路由器_tm.png differ diff --git a/datas/images/集线器--暗色.svg b/datas/images/集线器--暗色.svg new file mode 100644 index 0000000..fda9fec --- /dev/null +++ b/datas/images/集线器--暗色.svg @@ -0,0 +1,9 @@ + + + 集线器--暗色 + + + + + + \ No newline at end of file diff --git a/datas/images/集线器--暗色@1x.png b/datas/images/集线器--暗色@1x.png new file mode 100644 index 0000000..12356bf Binary files /dev/null and b/datas/images/集线器--暗色@1x.png differ diff --git a/datas/images/集线器--暗色@2x.png b/datas/images/集线器--暗色@2x.png new file mode 100644 index 0000000..418f181 Binary files /dev/null and b/datas/images/集线器--暗色@2x.png differ diff --git a/datas/images/集线器.png b/datas/images/集线器.png new file mode 100644 index 0000000..5b3de1a Binary files /dev/null and b/datas/images/集线器.png differ diff --git a/datas/images/集线器.svg b/datas/images/集线器.svg new file mode 100644 index 0000000..7ac89c9 --- /dev/null +++ b/datas/images/集线器.svg @@ -0,0 +1,1743 @@ + + + 集线器 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/datas/images/集线器@1x.png b/datas/images/集线器@1x.png new file mode 100644 index 0000000..3d204f3 Binary files /dev/null and b/datas/images/集线器@1x.png differ diff --git a/datas/images/集线器@2x.png b/datas/images/集线器@2x.png new file mode 100644 index 0000000..1e41c6c Binary files /dev/null and b/datas/images/集线器@2x.png differ diff --git a/datas/images/集线器_tm.png b/datas/images/集线器_tm.png new file mode 100644 index 0000000..cdae403 Binary files /dev/null and b/datas/images/集线器_tm.png differ diff --git a/datas/network.db b/datas/network.db new file mode 100644 index 0000000..bde5b24 Binary files /dev/null and b/datas/network.db differ diff --git a/extracted_questions.xlsx b/extracted_questions.xlsx new file mode 100644 index 0000000..922a8b8 Binary files /dev/null and b/extracted_questions.xlsx differ diff --git a/images/文字背景@3x.png b/images/文字背景@3x.png new file mode 100644 index 0000000..0c911a8 Binary files /dev/null and b/images/文字背景@3x.png differ diff --git a/Internet/main_X4.py b/main.py similarity index 57% rename from Internet/main_X4.py rename to main.py index d926f07..eac8de0 100644 --- a/Internet/main_X4.py +++ b/main.py @@ -2,16 +2,12 @@ import tkinter as tk from tkinter import * from tkinter import messagebox -from data_crud_sqlite import * +# from data_crud import * +from data_crud import * from data_line import Line_Right, line_draw, Useless_line -from data_dispose_CR import Show_Right_All -from data_envelop import EnvelopMove, revise_objshow, order_array -# 图片常量 -# img_host = PhotoImage(file='../images/主机_2.png') -# img_router = PhotoImage(file='../images/路由器_2.png') -# img_envelop = PhotoImage(file='../images/信封_2.png') -# img_detail = PhotoImage(file='../images/详情头_2.png') +from data_dispose_CR import Show_Right_All +from data_envelop import EnvelopMove # 创建父窗口函数 class Main_Gui(): @@ -22,15 +18,12 @@ class Main_Gui(): # 创建窗口界面 def Set_init_window(self): init_window_name = self.init_window_name - '''头歌虚拟平台上的屏幕长宽大概为: 宽:1360;高:690''' - # root_width = init_window_name.winfo_screenwidth() # 获取window屏幕宽度 - root_width = 1360 + root_width = init_window_name.winfo_screenwidth() # 获取window屏幕宽度 self.root_width = int(root_width / 4 * 3) # 设置父窗口界面宽度 - # print('父窗口界面宽度:'+str(self.root_width)) # 输出父窗口界面宽度 - # root_height = init_window_name.winfo_screenheight() # 获取window屏幕长度 - root_height = 690 + print('父窗口界面宽度:'+str(self.root_width)) # 输出父窗口界面宽度 + root_height = init_window_name.winfo_screenheight() # 获取window屏幕长度 self.root_height = int(root_height / 4 * 3) # 设置父窗口界面长度 - # print('父窗口界面长度:'+str(self.root_height)) # 输出父窗口界面长度 + print('父窗口界面长度:'+str(self.root_height)) # 输出父窗口界面长度 init_window_name.title('数据封包和解包模拟&X1-X4') # 标题 # 将窗口固定在屏幕中央 init_window_name.geometry(str(self.root_width)+'x'+str(self.root_height)+'+'+str(int((root_width-self.root_width)/2))+'+'+str(int((root_height-self.root_height)/2))) # 窗口大小 @@ -39,7 +32,6 @@ class Main_Gui(): filemenu = Menu(main_menu, tearoff=False) # 设置子菜单 filemenu.add_command(label='启动模拟', command=self.similar_packet) #模拟函数未完成 - filemenu.add_command(label='启动X4模拟', command=self.similar_packet_X4) # 需要建立函数 filemenu.add_command(label='查找', command=quit) # 寻址功能函数未完成 filemenu.add_separator() # 画线 filemenu.add_command(label='退出', command=quit) # 点击关闭页面 @@ -52,22 +44,20 @@ class Main_Gui(): self.cv = tk.Canvas(init_window_name, bg='#f0f0f0', # bg='white', - width=self.root_width-250, - height=self.root_height-150) + width=self.root_width-300, + height=self.root_height-180) self.cv.pack(side=tk.LEFT, anchor=tk.NW) # self.cv.grid(row=0, column=0) # self.cv.pack(anchor=tk.NW) # 画出包含仿真对象图的方框图 - self.cv.create_rectangle(100, 40, self.root_width - 250, self.root_height - 150, width=3, + self.cv.create_rectangle(140, 80, self.root_width - 300, self.root_height - 180, width=3, outline="#7f6000") # 这个位置可能需要修改 # 创建固定在左侧的标识符图像 - self.cv.create_image(50, 80, image=img_host) # 固定在左侧的主机对象 - self.cv.create_text(50, 110, text='主机', font=('蔚然雅黑', 14, 'bold'), fill='black') - self.cv.create_image(50, 160, image=img_router) # 固定在该canvas画布上的对象 == 路由器 - self.cv.create_text(50, 190, text='路由器', font=('蔚然雅黑', 14, 'bold'), fill='black') + self.cv.create_image(80, 150, image=img_host) # 固定在左侧的主机对象 + self.cv.create_text(80, 180, text='主机对象', font=('蔚然雅黑', 14, 'bold'), fill='black') + self.cv.create_image(80, 280, image=img_router) # 固定在该canvas画布上的对象 == 路由器 + self.cv.create_text(80, 310, text='路由器对象', font=('蔚然雅黑', 14, 'bold'), fill='black') - '''创建窗口前先将数据表simhost的数据恢复原样''' - revise_objshow('A','D') '''画出仿真移动对象的图''' self.Show_Canvas_Right() # 未完成 @@ -78,112 +68,11 @@ class Main_Gui(): self.under_show_all() # 未完成 - def similar_packet_X4(self): - # 删除窗口中的所有数据 - self.right_on_text.delete('1.0', tk.END) - self.right_under_text.delete('1.0', tk.END) - self.under_text.delete('1.0', tk.END) - self.cv.delete('detail');self.cv.delete('line') - # self.cv.unbind("",self.Mouse) # 清楚鼠标点击事件,修改中还未完成 2023-7-25 - self.open_envelop = False # 好像忘记了这个的作用了 - self.packet_emu_X4 = tk.Toplevel() - self.packet_emu_X4.title('X4-启动模拟') - self.packet_emu_X4.geometry('300x200+800+200') - # 建立数据模拟数据的输入框字体 - label_emu_x4_1 = tk.Label(self.packet_emu_X4, text='发送主机:').grid(row=0, pady=5) - label_emu_x4_2 = tk.Label(self.packet_emu_X4, text='接收主机:').grid(row=1, pady=5) - label_emu_x4_3 = tk.Label(self.packet_emu_X4, text='数据内容:').grid(row=2, pady=5) - label_emu_x4_4 = tk.Label(self.packet_emu_X4, text='数据大小(kb):').grid(row=3, pady=5) - label_emu_x4_5 = tk.Label(self.packet_emu_X4, text='数据标签:').grid(row=4, pady=5) - # 建立输入框 - self.entry_emu_x4_1 = tk.Entry(self.packet_emu_X4) - self.entry_emu_x4_1.grid(row=0, column=1) - - self.entry_emu_x4_2 = tk.Entry(self.packet_emu_X4) - self.entry_emu_x4_2.grid(row=1, column=1) - - self.entry_emu_x4_3 = tk.Entry(self.packet_emu_X4) - self.entry_emu_x4_3.grid(row=2, column=1) - - self.entry_emu_x4_4 = tk.Entry(self.packet_emu_X4) - self.entry_emu_x4_4.grid(row=3, column=1) - - self.entry_emu_x4_5 = tk.Entry(self.packet_emu_X4) - self.entry_emu_x4_5.grid(row=4, column=1) - - button_emu_left_X4 = tk.Button(self.packet_emu_X4, text='开启模拟', command=self.openpack_btn_x4).grid(row=5) # 函数未完成 2023-7-25 - return None - - # 设置X4模拟点击按钮后的函数 - def openpack_btn_x4(self): - # 先获取点击按钮后输入框中的数据 - entry_emu_x4_data_1 = self.entry_emu_x4_1.get() - entry_emu_x4_data_2 = self.entry_emu_x4_2.get() - entry_emu_x4_data_3 = self.entry_emu_x4_3.get() - entry_emu_x4_data_4 = self.entry_emu_x4_4.get() - entry_emu_x4_data_5 = self.entry_emu_x4_5.get() - - select_objlable = Select_ObjShow() # 查询数据库索取所有数据 - if entry_emu_x4_data_1=='' or entry_emu_x4_data_2=='' or entry_emu_x4_data_3==''or entry_emu_x4_data_4=='' or entry_emu_x4_data_5=='': - self.packet_emu_X4.destroy() - messagebox.showinfo('Error', '数据不能为空') - '''这个地方还需要添加一些其他的判断条件,暂且搁置''' - else: - self.packet_emu_X4.destroy() # 关闭启动模拟窗口 - self.AppPackTag_Data_X4 = { - 'SendHost': entry_emu_x4_data_1, 'ReceHost': entry_emu_x4_data_2, 'AppPackID': entry_emu_x4_data_3, 'AppPackSize': entry_emu_x4_data_4, 'AppPackTag': entry_emu_x4_data_5 - } - # print(self.AppPackTag_Data_X4) - self.move_number_x4 = int(self.AppPackTag_Data_X4['AppPackSize']) / 2048 - if self.move_number_x4 > 0 and self.move_number_x4 < 1: - self.move_number_x4 = 1 - elif self.move_number_x4 % 1 != 0: - self.move_number_x4 = int(self.move_number_x4 + 1) - # print('需要拆分成的数据包数量:'+str(self.move_number_x4)) - '''这个位置添加一个函数,通过获取的发送主机和接收主机的地址来修改数据库中ObjShow列中的数据''' - self.revise_objshow = revise_objshow(send_host=self.AppPackTag_Data_X4['SendHost'], reception_host=self.AppPackTag_Data_X4['ReceHost']) - '''这个位置调用信封移动的函数试试 PS:如果对象是如D——>A的则需要修改对象''' - # 如果需要修改数据之类的 - self.envelop_move_x4() - return None - - def envelop_move_x4(self): - '''在移动信封的同时需要设置在最下方显示进度提示,在最右边显示完成进度''' - '''建立一个暂时的变量,可以替换''' - self.unpacket = 0 - self.location_all_x4 = [] # 定义用来存放对象的位置 - self.Select_ObjID = Select_ObjID(self.revise_objshow) - for item in self.Select_ObjID: - # print(item) - ObjX = item['ObjX'];ObjY = item['ObjY'];ObjID = item['ObjID'];ObjType = item['ObjType'] - location = (ObjX, ObjY, ObjID, ObjType) - self.location_all_x4.append(location) - '''这个位置需要添加一个判断来判断是否需要将数组倒序''' - self.location_all_x4 = order_array(self.location_all_x4,self.AppPackTag_Data_X4['SendHost'],self.AppPackTag_Data_X4['ReceHost']) - # print(self.location_all_x4) - # 实现信封移动,并在实现的同时使最下方和最右方的canvas中显示数据 - '''这个位置需要通过获取的对象来获取值''' - # self.ObjShow_1_data = Select_ObjShow() - self.show_envelop = EnvelopMove(cv=self.cv, img_host=img_host, img_router=img_router, img_envelop=img_envelop, - img_detail=img_detail, move_data=self.location_all_x4, unpacket=self.unpacket, - move_number=self.move_number_x4, right_on_text=self.right_on_text, - Objshow_data=self.Select_ObjID, AppPackTag_Data=self.AppPackTag_Data_X4, - right_under_text=self.right_under_text, under_text=self.under_text) - self.show_envelop.main_envelop() - self.Mouse = self.show_envelop.Mouse_Event() - # self.show_envelop.show_data_move() - # print(self.ObjShow_1_data) - # print(self.location_all) - return None - - '''# 设置一个编写类封包函数''' def similar_packet(self): self.right_on_text.delete('1.0', tk.END) self.right_under_text.delete('1.0', tk.END) self.under_text.delete('1.0', tk.END) - self.cv.delete('detail');self.cv.delete('line') - # self.cv.unbind("", self.Mouse) # 定义一个值用来处理信封移动事件的开启 self.open_envelop = False # 建立一个子窗口完成数据 @@ -236,14 +125,14 @@ class Main_Gui(): 'AppPackSize': entry_emu_2_data, 'AppPackTag': entry_emu_3_data } - # print(self.AppPackTag_Data) + print(self.AppPackTag_Data) '''这里获取需要循环发送信封的次数''' self.move_number = int(self.AppPackTag_Data['AppPackSize']) / 2048 if self.move_number > 0 and self.move_number < 1: self.move_number = 1 elif self.move_number % 1 != 0: self.move_number = int(self.move_number + 1) - # print('需要拆分成的数据包数量:'+str(self.move_number)) + print('需要拆分成的数据包数量:'+str(self.move_number)) # print(self.host_date) # '''首先先建立一个判断两个主机之间是否存在通信的可能''' # 建立一个数组用来存储需要循环显示的数据 @@ -264,13 +153,11 @@ class Main_Gui(): ObjX = item['ObjX'];ObjY = item['ObjY'];ObjID = item['ObjID'];ObjType = item['ObjType'] location = (ObjX, ObjY, ObjID, ObjType) self.location_all.append(location) - # print(self.location_all) # 实现信封移动,并在实现的同时使最下方和最右方的canvas中显示数据 self.show_envelop = EnvelopMove(cv=self.cv,img_host=img_host, img_router=img_router, img_envelop=img_envelop,img_detail=img_detail,move_data=self.location_all,unpacket=self.unpacket, move_number=self.move_number,right_on_text=self.right_on_text,Objshow_data=self.ObjShow_1_data,AppPackTag_Data=self.AppPackTag_Data, right_under_text=self.right_under_text,under_text=self.under_text) self.show_envelop.main_envelop() - self.Mouse = self.show_envelop.Mouse_Event() # self.show_envelop.show_data_move() # print(self.ObjShow_1_data) # print(self.location_all) @@ -281,7 +168,7 @@ class Main_Gui(): self.simhost_all_data = get_simhost_alldata() # 获取simhost表中的所有数据 # print(self.simhost_all_data) self.ObjShow_1_data = Select_ObjShow() # 采集所有ObjShow为1的数据 - # print(self.ObjShow_1_data) + print(self.ObjShow_1_data) self.line_data = line_draw(self.ObjShow_1_data) # print(self.line_data) '''外接画线函数''' @@ -301,13 +188,11 @@ class Main_Gui(): self.right_on_label = tk.Label(self.init_window_name, text='发送主机详情:',font=('蔚然雅黑', 12, 'bold')) # self.right_on_label.pack(side=tk.TOP, fill=tk.Y, padx=10, pady=10) self.right_on_label.pack(anchor=tk.NW, fill=tk.Y, padx=5, pady=5) - # self.right_on_text = tk.Text(self.init_window_name, width=29, height=13) - self.right_on_text = tk.Text(self.init_window_name, width=24, height=10) + self.right_on_text = tk.Text(self.init_window_name, width=40, height=22) self.right_on_text.pack(anchor=tk.NW, fill=tk.Y, padx=5, pady=5) self.right_under_label = tk.Label(self.init_window_name, text='接收主机详情:', font=('蔚然雅黑', 12, 'bold')) self.right_under_label.pack(anchor=tk.NW, fill=tk.Y, padx=5, pady=5) - # self.right_under_text = tk.Text(self.init_window_name, width=29, height=13) - self.right_under_text = tk.Text(self.init_window_name, width=24, height=10) + self.right_under_text = tk.Text(self.init_window_name, width=40, height=22) self.right_under_text.pack(anchor=tk.NW, fill=tk.Y, padx=5, pady=5) self.right_on_text.configure(font=("TkDefaultFont", 12)) self.right_under_text.configure(font=("TkDefaultFont", 12)) @@ -334,16 +219,15 @@ class Main_Gui(): def under_show_all(self): '''更换方式显示''' scrollbar_y = tk.Scrollbar(self.init_window_name) - scrollbar_y.place(x=760, y=380,relheight=0.25) + scrollbar_y.place(x=1130, y=638,relheight=0.21) # scrollbar_x = tk.Scrollbar(self.init_window_name, orient=tk.HORIZONTAL) # scrollbar_x.pack(side=tk.BOTTOM, fill=tk.X) - self.under_label = tk.Label(self.init_window_name, text='数据传输: ', font=('蔚然雅黑', 12, 'bold')) + self.under_label = tk.Label(self.init_window_name, text='数据传输进度: ', font=('蔚然雅黑', 12, 'bold')) # self.under_label.pack(side=tk.BOTTOM, anchor=tk.SW) - self.under_label.place(x=10, y=375) - # self.under_text = tk.Text(self.init_window_name, width=82, height=8) - self.under_text = tk.Text(self.init_window_name, width=65, height=8) + self.under_label.place(x=20, y=638) + self.under_text = tk.Text(self.init_window_name, width=123, height=10) # self.under_text.place(x=140, y=638, relheight=1) - self.under_text.place(x=100, y=375) + self.under_text.place(x=140, y=638) self.under_text.configure(font=("TkDefaultFont", 12)) scrollbar_y.config(command=self.under_text.yview) self.under_text.config(yscrollcommand=scrollbar_y.set) @@ -363,19 +247,10 @@ class Main_Gui(): if __name__ == '__main__': init_Windows = tk.Tk() # 创建一个父窗口 - create_simhost() - create_ipaddr() - insert_simhost() - insert_ipaddr() - img_host = PhotoImage(file='/data/workspace/myshixun/images/主机_2.png') - img_router = PhotoImage(file='/data/workspace/myshixun/images/路由器_2.png') - img_envelop = PhotoImage(file='/data/workspace/myshixun/images/信封_2.png') - img_detail = PhotoImage(file='/data/workspace/myshixun/images/详情头_2.png') - # img_host = PhotoImage(file='../images/主机_2.png') - # img_router = PhotoImage(file='../images/路由器_2.png') - # img_envelop = PhotoImage(file='../images/信封_2.png') - # img_detail = PhotoImage(file='../images/详情头_2.png') + img_host = PhotoImage(file='./images/主机_1.png') + img_router = PhotoImage(file='./images/路由器_1.png') + img_envelop = PhotoImage(file='./images/信封_1.png') + img_detail = PhotoImage(file='./images/详情头_1.png') Gui = Main_Gui(init_Windows) Gui.Set_init_window() - print('程序运行成功!') - init_Windows.mainloop() + init_Windows.mainloop() \ No newline at end of file diff --git a/new/AgreementUtil.py b/new/AgreementUtil.py new file mode 100644 index 0000000..d9e330f --- /dev/null +++ b/new/AgreementUtil.py @@ -0,0 +1,107 @@ +import socket +import struct + +def format_mac_address(mac_bytes): + """ + Format a MAC address from bytes to a human-readable string. + + Args: + mac_bytes (bytes): The MAC address in bytes format. + + Returns: + str: The MAC address in human-readable format. + """ + mac_str = mac_bytes.hex() + mac_formatted = ':'.join(mac_str[i:i+2] for i in range(0, 12, 2)) + return mac_formatted + +class AgreementUtil(): + + @staticmethod + def create_udp_packet(data, dest_port, src_port): + """ + 创建UDP数据包 + Returns: + bytes: `UDP数据包`. + """ + # UDP header fields + udp_header = struct.pack('!HHHH', src_port, dest_port, 8 + len(data), 0) # Length includes header length + return udp_header + data.encode() + + @staticmethod + def create_ip_packet(udp_packet, dest_ip, src_ip): + """ + 创建IP数据包 + Returns: + bytes: IP数据包 + """ + # IP header fields + version = 4 + ihl = 5 # Internet Header Length + type_of_service = 0 + total_length = 20 + len(udp_packet) # 20 bytes IP header + UDP packet length + identification = 54321 + flags = 0 # Don't fragment + fragment_offset = 0 + ttl = 64 # Time to live + protocol = 17 # UDP + checksum = 0 # For simplicity, set to 0 + source_ip = socket.inet_aton(src_ip) + dest_ip = socket.inet_aton(dest_ip) + # IP数据包头部字段 + ip_header = struct.pack('!BBHHHBBH4s4s', + (version << 4) + ihl, type_of_service, total_length, + identification, (flags << 13) + fragment_offset, + ttl, protocol, checksum, source_ip, dest_ip) + return ip_header + udp_packet + + @staticmethod + def create_ethernet_frame(ip_packet, dest_mac, src_mac): + """ + 创建以太网帧数据包 + Returns: + bytes: 以太网帧数据包 + """ + # Convert MAC addresses from string format to bytes + dest_mac_bytes = bytes.fromhex(dest_mac.replace(':', '')) + src_mac_bytes = bytes.fromhex(src_mac.replace(':', '')) + # Ethernet type for IP (0x0800) + ethernet_type = 0x0800 + # Pack Ethernet frame fields + ethernet_frame = struct.pack('!6s6sH', dest_mac_bytes, src_mac_bytes, ethernet_type) + return ethernet_frame + ip_packet + + @staticmethod + def parse_ethernet_frame(frame): + """ + 解析以太网帧数据包 + Returns: 发送端MAC,接收端MAC,以太网类型,IP数据包 + """ + dest_mac, src_mac, ethertype = struct.unpack('!6s6sH', frame[:14]) + payload = frame[14:] + return format_mac_address(src_mac), format_mac_address(dest_mac), ethertype, payload + + @staticmethod + def parse_ip_packet(packet): + """ + 解析 IP 数据包 + Returns: 发送端IP,接收端IP,协议,UDP数据包 + """ + version_ihl, type_of_service, total_length, identification, flags_fragment_offset, \ + ttl, protocol, checksum, source_ip, dest_ip = struct.unpack('!BBHHHBBH4s4s', packet[:20]) + payload = packet[20:] + return socket.inet_ntoa(source_ip), socket.inet_ntoa(dest_ip), protocol, payload + + @staticmethod + def parse_udp_packet(packet): + """ + 解析UDP数据包 + Returns: + tuple: 发送主机端口,接收主机端口,数据 + """ + src_port, dest_port, length, checksum = struct.unpack('!HHHH', packet[:8]) + data = packet[8:] + return src_port, dest_port, data.decode() + + + diff --git a/new/NetworkAnalog.py b/new/NetworkAnalog.py new file mode 100644 index 0000000..9e118a9 --- /dev/null +++ b/new/NetworkAnalog.py @@ -0,0 +1,421 @@ +import ipaddress +import sys +import threading +from tkinter import filedialog + +from SimUtil import * + +from SimObjs import SimPacket, SimHost, AllSimConnect, SimRouter, SimSwitch, SimHub, SimBase +from dbUtil import search, execute_sql, delete_obj, truncate_db +from PIL import Image as imim +from packet import * + + +class Message(Canvas): + def __init__(self, master, **kwargs): + super().__init__(master, **kwargs) + self.message = [] + self.master = master + self.scrollbar_y = tk.Scrollbar(master, orient="vertical", command=self.yview) + self.scrollbar_y.pack(side="right", fill=BOTH) + self.scrollbar_x = tk.Scrollbar(master, orient=HORIZONTAL, command=self.xview) + self.scrollbar_x.pack(side="bottom", fill=BOTH) + self.config(yscrollcommand=self.scrollbar_y.set, xscrollcommand=self.scrollbar_x.set) + + def show_message(self, message, color="#5fa8fe"): + for mes in message.split("\n"): + self.message.append({"message": mes, "color": color}) + num = 0 + self.delete("message") + for function in self.message: + self.create_text(20, 25 * num + 10, anchor="nw", text=function["message"], font=("", 15), + fill=function["color"], tags="message") + num += 1 + self.update() + self.configure(scrollregion=self.bbox("all")) + self.scrollbar_y.set(1.0, 1.0) + self.yview_moveto(1.0) + + +class NetWorkAnalog(Canvas): + def __init__(self, master, **kwargs): + super().__init__(master, **kwargs) + self.master = master + self.router_img = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/路由器@2x.png").resize((40, 40))) + self.switch_img = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/交换机@2x.png").resize((40, 40))) + self.hub_img = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/集线器@2x.png").resize((40, 40))) + self.host_img = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/主机@2x.png").resize((40, 40))) + + self.width = int(self.cget("width")) + self.height = int(self.cget("height")) + self.canvas_size = [150, 0, int(self.width * 0.8), int(self.height * 0.8)] + self.label_top_img = ImageTk.PhotoImage(imim.open("../datas/images/右上框@2x.png").resize((int(self.width - self.width * 0.85), int(self.canvas_size[3] * 0.4)))) + self.label_bottom_img = ImageTk.PhotoImage(imim.open("../datas/images/右下框@2x.png").resize((int(self.width - self.width * 0.85), int(self.canvas_size[3] * 0.45)))) + self.message_img = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/日志消息展示框@2x.png").resize((self.width - 60, self.height - self.canvas_size[3] - 30)) + ) + self.text_back_img = ImageTk.PhotoImage( + Image.open("../images/文字背景@3x.png").resize((155, 90)) + ) + message_frame = Frame(self, width=self.width - 60, height=self.height - self.canvas_size[3] - 30) + message_frame.place(x=15, y=self.canvas_size[3] + 15, anchor=NW) + self.message = Message(message_frame, width=self.width - 60, height=self.height - self.canvas_size[3] - 30) + self.message.pack() + self.message.configure(bg="#edf8ff") + self.configure(bg="#ddeafe") + self.filename = ImageTk.PhotoImage(imim.open("../datas/images/背景@2x.png").resize((self.width, self.canvas_size[3]))) + self.create_image(0, 0, image=self.filename, anchor=NW) + self.chose = self.host_img + self.AllSimObjs = {} + self.conns = [] + self.drawLine = True # 画线标志 + self.line_start_obj = None + self.line_end_obj = None + self.line_start_ifs = None + self.line_end_ifs = None + self.chose_obj = None + self.show_label_flag = True + self.show_interface_flag = True + self.trans_obj = set() + self.create_widget() + + def is_chose(self): + """ + 当被选中时,绘制选中框 + :return: + """ + self.delete("rectangle") + self.create_rectangle(self.chose_obj.ObjX - 35, self.chose_obj.ObjY - 35, self.chose_obj.ObjX + 15, + self.chose_obj.ObjY + 15, outline="red", tags="rectangle") + + def reload_data(self): + # todo: 加载上一次程序运行的数据 + """ + 加载上一次程序运行时的数据 + :return: + """ + self.AllSimObjs = {} + self.conns = [] + sim_obj_sql = "select * from sim_objs" + sim_obj_data = search(sim_obj_sql) + for index, row in sim_obj_data.iterrows(): # 初始化组件对象 + sim_type = row["ObjType"] + ObjX = row["ObjX"] + ObjY = row["ObjY"] + ConfigCorrect = row["ConfigCorrect"] + ObjLable = row["ObjLabel"] + ObjID = row["ObjID"] + if sim_type == 1: + tag = SimHost(self, ObjX, ObjY, ObjID, ConfigCorrect, ObjLable) + elif sim_type == 2: + tag = SimRouter(self, ObjX, ObjY, ObjID, ConfigCorrect, ObjLable) + elif sim_type == 3: + tag = SimSwitch(self, ObjX, ObjY, ObjID, ConfigCorrect, ObjLable) + else: + tag = SimHub(self, ObjX, ObjY, ObjID, ConfigCorrect, ObjLable) + tag.create_img() + self.AllSimObjs[tag.ObjID] = tag + + sim_conn_sql = "select s.conn_id, ConfigCorrect, node_id, node_ifs from sim_conn s join conn_config c on s.conn_id=c.conn_id" + sim_conn_data = search(sim_conn_sql) + conn_datas = {} + for index, conn in sim_conn_data.iterrows(): + if (conn["conn_id"], conn["ConfigCorrect"]) not in conn_datas: + conn_datas[(conn["conn_id"], conn["ConfigCorrect"])] = [(conn["node_id"], conn["node_ifs"])] + else: + conn_datas[(conn["conn_id"], conn["ConfigCorrect"])].append((conn["node_id"], conn["node_ifs"])) + for key, value in conn_datas.items(): + conn_obj = AllSimConnect(self, self.AllSimObjs[value[0][0]], value[0][1], + self.AllSimObjs[value[1][0]], value[1][1], key[1]) + conn_obj.draw_line() + self.AllSimObjs[value[0][0]].connections[value[0][1] - 1] = conn_obj # 将连接对象传入组件对象 + self.AllSimObjs[value[1][0]].connections[value[1][1] - 1] = conn_obj + self.conns.append(conn_obj) + conn_obj.ConfigCorrect = key[1] + + def show_obj(self, AllSimObj, conns): + self.AllSimObjs = AllSimObj + self.conns = conns + for key, sim_obj in self.AllSimObjs.items(): + sim_obj.create_img() + for conn in self.conns: + conn.draw_line() + + def send_packet(self): + """ + 发送数据包 + :return: + """ + hosts = {} + for key, tag in self.AllSimObjs.items(): + if tag.ObjType == 1 and tag.ConfigCorrect == 1: + hosts[tag.ObjLabel] = tag.ObjID + child2 = tk.Toplevel() + child2.title("数据包配置") + child2.geometry('392x306+200+110') + child2.grab_set() # 设置组件焦点抓取。使焦点在释放之前永远保持在这个组件上,只能在这个组件上操作 + tk.Label(child2, text='发送主机:', font=('黑体', 16)).grid(row=0, column=0, columnspan=2, sticky='w', pady=10) + send_host = StringVar() + host_combobox = ttk.Combobox( + master=child2, # 父容器 + height=5, # 高度,下拉显示的条目数量 + width=18, # 宽度 + state='readonly', # readonly(只可选) + font=('', 16), # 字体 + textvariable=send_host, # 通过StringVar设置可改变的值 + values=list(hosts.keys()), # 设置下拉框的选项 + ) + + host_combobox.grid(row=0, column=1, pady=10) + tk.Label(child2, text='接收主机:', font=('黑体', 16)).grid(row=1, column=0, sticky='w', pady=10) # ,sticky='w'靠左显示 + receive_host = StringVar() + ttk.Combobox( + master=child2, # 父容器 + height=5, # 高度,下拉显示的条目数量 + width=18, # 宽度 + state='readonly', # readonly(只可选) + font=('', 16), # 字体 + textvariable=receive_host, # 通过StringVar设置可改变的值 + values=list(hosts.keys()), # 设置下拉框的选项 + ).grid(row=1, column=1, pady=10) + tk.Label(child2, text='数据大小(kb):', font=('黑体', 16)).grid(row=2, column=0, sticky='w', + pady=10) # ,sticky='w'靠左显示 + packet_size = tk.Entry(child2, font=('黑体', 16), textvariable=tk.StringVar()) + packet_size.grid(row=2, column=1, pady=10) + tk.Label(child2, text='数据标签:', font=('黑体', 16)).grid(row=3, column=0, sticky='w', + pady=10) # ,sticky='w'靠左显示 + packet_label = tk.Entry(child2, font=('黑体', 16), textvariable=tk.StringVar()) + packet_label.grid(row=3, column=1, pady=10) + + def send(): + """ + 发送数据包 + :return: + """ + if send_host.get() == "" or receive_host.get() == "" or packet_size.get() == "" or packet_label.get() == "": + messagebox.showerror("注意", message="信息不能为空") + return + if send_host.get() == receive_host.get(): + messagebox.showerror("注意", message="发送主机和接收主机不能相同!") + send_message = """发送主机:{}\n\nIP地址:{}\n\nMac地址:{}\n\n发送数据包大小:{}\n\n已发送数据数量:{}\n\n需要发送的数据包总数:{}""" + receive_message = """接收主机:{}\n\nIP地址:{}\n\nMac地址:{}\n\n发送数据包大小:{}\n\n已接收数据数量:{}\n\n需要发送的数据包总数:{}""" + send_host_obj = self.AllSimObjs[hosts[send_host.get()]] + receive_host_obj = self.AllSimObjs[hosts[receive_host.get()]] + pack_label = packet_label.get() + pack_size = packet_size.get() + child2.destroy() + count = split_appdata(int(pack_size)) + self.show_top_message(send_host_obj, + message=send_message.format(send_host_obj.ObjLabel, send_host_obj.interface[0]["ip"], + send_host_obj.interface[0]["mac"], pack_size, 0, count)) + self.show_bottom_message(receive_host_obj, message=receive_message.format(receive_host_obj.ObjLabel, + receive_host_obj.interface[0][ + "ip"], + receive_host_obj.interface[0][ + "mac"], pack_size, 0, count)) + for obj in self.trans_obj: + self.delete(obj.ObjID + "detail_button") + self.delete(obj.ObjID + "detail") + self.unbind(obj.ObjID, "") + send_host_obj.create_packet(receive_host_obj, pack_label, pack_size) + + tk.Button(child2, text='开启模拟', font=('黑体', 16), height=1, command=send).grid(row=5, column=0, sticky='e', pady=10) + tk.Button(child2, text='取消', font=('黑体', 16), height=1, command=child2.destroy).grid(row=5, column=1, sticky='e', pady=10) + # 建立画出对象详情的函数 + def draw_detail(self, obj): + # 判断type对象来画出详情表 + if obj.ObjType == 1: # 当type对象为1 画出主机详情图 + app_color = ("#94D050", '#00B0F0') + trans_color = ("#94D050", '#00B0F0') + ip_color = ("#94D050", '#00B0F0') + mac_color = ("#94D050", '#00B0F0') + elif obj.ObjType == 2: + app_color = ("#D3D3D3", "#D3D3D3") + trans_color = ("#D3D3D3", "#D3D3D3") + ip_color = ("#94D050", '#00B0F0') + mac_color = ("#94D050", '#00B0F0') + elif obj.ObjType == 3: + app_color = ("#D3D3D3", "#D3D3D3") + trans_color = ("#D3D3D3", "#D3D3D3") + ip_color = ("#D3D3D3", "#D3D3D3") + mac_color = ("#94D050", '#00B0F0') + else: + app_color = ("#D3D3D3", "#D3D3D3") + trans_color = ("#D3D3D3", "#D3D3D3") + ip_color = ("#D3D3D3", "#D3D3D3") + mac_color = ("#94D050", '#00B0F0') + frist_x = obj.ObjX # 获取frist_x + frist_y = obj.ObjY # 获取frist_y + # 这里缺少一个删除其他详情的步骤 + # 画出连接详情表的线 + tag_id = obj.ObjID + "detail" + if len(self.gettags(tag_id)) > 0: + self.delete(tag_id) + else: + self.create_line((frist_x + 40, frist_y - 20), (frist_x + 80, frist_y - 20), + (frist_x + 90, frist_y - 60), + fill='#50abf8', tags=tag_id, width=2) + # 画出详情表 + self.create_image(frist_x + 50, frist_y - 140, image=self.text_back_img, anchor=NW, tags=tag_id) + # 画出相应的绿条数据 + self.create_text(frist_x + 70, frist_y - 130, text='应用层', tags=tag_id) + self.create_text(frist_x + 70, frist_y - 107, text='传输层', tags=tag_id) + self.create_text(frist_x + 70, frist_y - 84, text='IP 层', tags=tag_id) + self.create_text(frist_x + 70, frist_y - 62, text='链路层', tags=tag_id) + # 画出 右侧绿色和蓝色的类进度条 + # 应用层 + self.create_rectangle(frist_x + 197, frist_y - 135, frist_x + 170, frist_y - 120, fill=app_color[0], + outline=app_color[0], tags=tag_id) + self.create_rectangle(frist_x + 168, frist_y - 135, frist_x + 148, frist_y - 120, fill=app_color[1], + outline=app_color[1], tags=tag_id) + # 传输层 + self.create_rectangle(frist_x + 197, frist_y - 115, frist_x + 160, frist_y - 100, + fill=trans_color[0], + outline=trans_color[0], tags=tag_id) + self.create_rectangle(frist_x + 158, frist_y - 115, frist_x + 133, frist_y - 100, + fill=trans_color[1], + outline=trans_color[1], tags=tag_id) + # IP 层 + self.create_rectangle(frist_x + 197, frist_y - 93, frist_x + 150, frist_y - 78, fill=ip_color[0], + outline=ip_color[0], tags=tag_id) + self.create_rectangle(frist_x + 148, frist_y - 93, frist_x + 118, frist_y - 78, fill=ip_color[1], + outline=ip_color[1], tags=tag_id) + # 链路层 + self.create_rectangle(frist_x + 197, frist_y - 70, frist_x + 133, frist_y - 55, fill=mac_color[0], + outline=mac_color[0], tags=tag_id) + self.create_rectangle(frist_x + 131, frist_y - 70, frist_x + 98, frist_y - 55, fill=mac_color[1], + outline=mac_color[1], tags=tag_id) + + def trans_over(self, objs): + print("传输完成") + self.trans_obj = objs + for obj in objs: + obj.create_detail_button() + self.tag_bind(obj.ObjID, "", lambda event, obj=obj: self.draw_detail(obj)) # 绑定右击事件 + obj.is_packet = False + obj.is_un_packet = False + + def clear_canvas(self): + """ + 清除画布 + :return: + """ + ask = messagebox.askquestion(title='确认操作', message='确认要清除画布吗?') + if ask == "no": + return + truncate_db() # 清除数据库 + for tag_id, tag in self.AllSimObjs.items(): + self.delete(tag_id) + self.delete(tag_id + "text") + for conn in self.conns: + conn.delete_line() + self.delete("rectangle") + self.AllSimObjs.clear() + self.conns.clear() + + def export_data(self): + try: + export = ExportUtil(sys.path[0] + "/database.xlsx") + export.export() + self.message.show_message("文件导出成功!文件位置在{}".format(sys.path[0] + "/database.xlsx")) + except Exception as E: + self.message.show_message(str(E), color="red") + + def import_data(self): + truncate_db() + for tag_id, tag in self.AllSimObjs.items(): + self.delete(tag_id) + self.delete(tag_id + "text") + for conn in self.conns: + conn.delete_line() + self.delete("rectangle") + self.AllSimObjs.clear() + self.conns.clear() + file_path = filedialog.askopenfilename() + if file_path: + export = ExportUtil(file_path) + export.import_data() + self.message.show_message("文件导入成功!") + self.reload_data() + + def show_top_message(self, obj, message): + # todo: 显示网络配置信息 + """ + 显示网络配置信息 + :return: + """ + self.delete("netSet") + self.create_text(self.width * 0.82 + 120, 80, text="(" + obj.ObjLabel + ")", anchor="n", font=('微软雅黑', 14, 'bold'), + fill="#7030a0", tags="netSet") + self.create_text(self.width * 0.82 + 20, 80 + 25, text=message, + anchor="nw", font=('宋体', 14), tags="netSet") + + def show_bottom_message(self, obj, message): + # todo: 显示路由表交换表信息 + """ + 显示路由交换表信息 + :return: + """ + self.delete("routerSet") + self.create_text(self.width * 0.82 + 120, self.canvas_size[3] / 2 + 50, text="(" + obj.ObjLabel + ")", anchor="n", font=('微软雅黑', 14, 'bold'), + fill="#7030a0", tags="routerSet") + self.create_text(self.width * 0.82 + 20, self.canvas_size[3] / 2 + 50 + 25, text=message, + anchor="nw", font=('宋体', 14), tags="routerSet") + + def create_config_button(self): + # 创建一个菜单栏,这里我们可以把他理解成一个容器,在窗口的上方 + menubar = tk.Menu(root) + # 定义一个空菜单单元 + setMenu = tk.Menu(menubar, tearoff=0) + menubar.add_cascade(label='发送数据包', menu=setMenu) + setMenu.add_command(label='发送数据包', command=self.send_packet) + menubar.add_command(label='清除屏幕', command=self.clear_canvas) + menubar.add_command(label='导出数据', command=self.export_data) + menubar.add_command(label='导入数据', command=self.import_data) + root.config(menu=menubar) + + def create_widget(self): + # todo: 创建页面 + """ + 创建整体页面布局 + :return: + """ + self.create_rectangle(self.canvas_size[0], self.canvas_size[1], self.canvas_size[2], self.canvas_size[3], outline="#7f6000", width=0) # 矩形框,左上角坐标,右下角坐标 + self.create_image(self.width * 0.82, 30, image=self.label_top_img, anchor=NW) # 矩形框,左上角坐标,右下角坐标 + self.create_text(self.width * 0.82 + 120, 30 + 15, text="发送主机详情", anchor="n", font=('微软雅黑', 18, 'bold')) + self.create_image(self.width * 0.82, self.canvas_size[3] / 2, image=self.label_bottom_img, anchor=NW) # 矩形框,左上角坐标,右下角坐标 + # round_rectangle(self, self.width * 0.82, self.canvas_size[3] / 2, self.width * 0.98, self.canvas_size[3] - 30, outline="#ffff00", width=2, fill="#f4b88e") # 矩形框,左上角坐标,右下角坐标 + self.create_text(self.width * 0.82 + 120, self.canvas_size[3] / 2 + 15, text="接收主机详情", anchor="n", font=('微软雅黑', 18, 'bold')) + # 显示左边的固定图片 + img_height, text_height, split_width = 50, 40, 120 + self.create_text(text_height, split_width + 40, text="路由器", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字 + self.create_image(img_height, split_width, image=self.router_img, anchor="nw") + self.create_text(text_height, split_width * 2 + 40, text="交换机", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字 + self.create_image(img_height, split_width * 2, image=self.switch_img, anchor="nw") + self.create_text(text_height, split_width * 3 + 40, text="集线器", anchor="nw", font=('微软雅黑', 18, 'bold')) # 显示文字 + 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')) # 显示文字 + self.create_image(img_height, split_width * 4, image=self.host_img, anchor="nw") + + +if __name__ == '__main__': + root = Window() + root.title('网络拓扑图') + screen_width = root.winfo_screenwidth() # winfo方法来获取当前电脑屏幕大小 + screen_height = root.winfo_screenheight() + root_attr = { + "width": screen_width * 0.83, + "height": screen_height * 0.85, + } + size = '%dx%d+%d+%d' % (root_attr['width'], root_attr['height'], (screen_width - root_attr['width']) / 2, + (screen_height - 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') + canvas.create_config_button() + canvas.reload_data() + root.geometry(size) + root.mainloop() diff --git a/new/SimObjs.py b/new/SimObjs.py new file mode 100644 index 0000000..47c7f04 --- /dev/null +++ b/new/SimObjs.py @@ -0,0 +1,600 @@ +import math +import sys +import threading +from time import sleep +from tkinter import Tk +from tkinter import messagebox + +from ttkbootstrap import * +from uuid import uuid4 +import ipaddress +import time + +from PIL import ImageTk, Image, ImageFont + +from dbUtil import search +from packet import * + +pause_event = threading.Event() + +class SimBase(): + # todo: 组件父类 + """ + 图标类,所有组件的父类 + """ + def __init__(self, canvas, x, y, id, config=None, label=None): + self.ConfigCorrect = 0 if config is None else config # 是否进行配置 + self.ObjType = None # 组件类型 1->主机,2->路由器,3->交换机,4->集线器 + self.ObjLabel = "" + self.ObjID = id + self.ObjX = x + self.ObjY = y + self.canvas = canvas + self.is_packet = False + self.is_un_packet = False + self.host_img = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/主机@2x.png").resize((40, 40))) + self.host_img_tm = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/主机--暗色@2x.png").resize((40, 40))) + self.router_img = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/路由器@2x.png").resize((40, 40))) + self.router_img_tm = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/路由器--暗色@2x.png").resize((40, 40))) + self.switch_img = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/交换机@2x.png").resize((40, 40))) + self.switch_img_tm = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/交换机--暗色@2x.png").resize((40, 40))) + self.hub_img = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/集线器@2x.png").resize((40, 40))) + self.hub_img_tm = ImageTk.PhotoImage( + Image.open(sys.path[0] + "/../datas/images/集线器--暗色@2x.png").resize((40, 40))) + self.img = None + self.img_tm = None + self.interface = [{}, {}, {}, {}] + self.connections = ["1", "2", "3", "4"] + self.set_default_config() + self.packet_window = None + + def set_default_name(self): + data_frame = search(f"select objid from sim_objs where objType={self.ObjType}") + num = data_frame.size + 1 + if isinstance(self, SimHost): + name = "SHO%d" % num + elif isinstance(self, SimRouter): + name = "SRO%d" % num + elif isinstance(self, SimSwitch): + name = "SWI%d" % num + else: + name = "SHUB%d" % num + return name + + def create_img(self): + """ + 创建图片 + :return: + """ + if self.ObjType == 1: + self.img = self.host_img + self.img_tm = self.host_img_tm + elif self.ObjType == 2: + self.img = self.router_img + self.img_tm = self.router_img_tm + elif self.ObjType == 3: + self.img = self.switch_img + self.img_tm = self.switch_img_tm + else: + self.img = self.hub_img + self.img_tm = self.hub_img_tm + self.canvas.delete("L") + id = self.canvas.create_image(self.ObjX - 30, self.ObjY - 30, + image=self.img if self.ConfigCorrect == 1 else self.img_tm, anchor="nw", + tags=self.ObjID) + self.canvas.dtag("L", "L") + self.canvas.create_text(self.ObjX - 20, self.ObjY - 60, text=self.ObjLabel, font=("", 16, "bold"), + fill="#9b78eb", tags=self.ObjID + "text", anchor="nw") + if self.ObjType == 1: + self.canvas.create_text(self.ObjX - 45, self.ObjY + 15, text=self.interface[0]["ip"]if len(self.interface) > 0 else "", font=("", 16, "bold"), + fill="#9b78eb", tags=self.ObjID + "text", anchor="nw") + self.canvas.create_text(self.ObjX - 45, self.ObjY + 35, text=self.interface[0]["mac"]if len(self.interface) > 0 else "", font=("", 16, "bold"), + fill="#9b78eb", tags=self.ObjID + "text", anchor="nw") + self.canvas.create_text(self.ObjX - 45, self.ObjY + 55, text=self.interface[0]["addr"]if len(self.interface) > 0 else "", font=("", 16, "bold"), + fill="#9b78eb", tags=self.ObjID + "text", anchor="nw") + self.canvas.tag_raise(id) + + def set_default_config(self): + sql = f"select * from conn_config where node_id='{self.ObjID}'" + conn_data = search(sql) + for index, conn in conn_data.iterrows(): + self.interface[int(conn["node_ifs"]) - 1] = {"ip": conn["ip"], + "mac": conn["mac"], + "conn_port": conn["conn_port"], + "addr": conn["addr"]} + + def get_config(self): + sql = f"select * from conn_config where node_id='{self.ObjID}'" + conn_data = search(sql) + return conn_data + + def transfer_animate(self, status, packet, packet_size, error_message=None): + if status: + text = f"消息大小: {packet_size}\n" \ + f"消息内容: {packet}" + self.canvas.create_rectangle(self.ObjX + 30, self.ObjY - 30, self.ObjX + 160, self.ObjY + 20, + outline="#92d050", + width=3, fill="#92d050", tags=self.ObjID + "packetData") + self.canvas.create_text(self.ObjX + 35, self.ObjY - 25, text=text, anchor="nw", + font=('', 10), tags=self.ObjID + "packetData") # 显示文字 + self.canvas.update() + sleep(2) + self.canvas.delete(self.ObjID + "packetData") # 删除展示的数据包内容 + else: + text = f"传输失败\n" if error_message is None else error_message + self.canvas.create_rectangle(self.ObjX + 30, self.ObjY - 30, self.ObjX + 160, self.ObjY, outline="red", + width=3, fill="red", tags=self.ObjID + "packetData") + self.canvas.create_text(self.ObjX + 35, self.ObjY - 25, text=text, anchor="nw", + font=('', 10), tags=self.ObjID + "packetData") # 显示文字 + self.canvas.update() + sleep(2) + self.canvas.delete(self.ObjID + "packetData") # 删除展示的数据包内容 + + def create_detail_button(self): + frist_x = self.ObjX # 获取frist_x + frist_y = self.ObjY # 获取frist_y + tag_id = self.ObjID + "detail_button" + self.canvas.create_rectangle(frist_x + 10, frist_y - 25, frist_x + 40, frist_y - 15, fill="#f0a732", + outline="#47b2ff",width=2, tags=tag_id) + + def __str__(self): + str = "" + config = self.get_config() + for index, data in config.iterrows(): + str += f"【接口{data['node_ifs']}】\n" + str += f"IP: {data['ip']}\n" + str += f"MAC: {data['mac']}\n" + return str + + +class SimPacket(): + # todo: 数据包类 + """ + 数据包类 + """ + def __init__(self, source_ip, source_mac, destination_ip, destination_mac, message, label, size, sendObj, receiveObj): + """ + :param source_mac: 源主机mac地址 + :param source_ip: 源主机ip地址 + :param destination_mac: 目的主机mac地址 + :param destination_ip: 目的主机ip地址 + :param message: 数据 + """ + self.source_mac = source_mac + self.source_ip = source_ip + self.sendObj = sendObj + self.receiveObj = receiveObj + self.destination_mac = destination_mac + self.destination_ip = destination_ip + self.message = message + self.label = label + self.size = size + self.up_jump = None # 上一跳连接对象 + self.objs = set() + self.img = None + self.id = str(uuid4()) + self.process_img() + + def process_img(self): + img = Image.open(sys.path[0] + "/../datas/images/packet.png").resize((30, 30)) + draw = ImageDraw.Draw(img) + font = ImageFont.truetype("arial.ttf", size=16) + text_width, text_height = draw.textsize(str(self.label), font=font) + # 计算文本在右上角的位置 + x = img.width - text_width - 10 # 右上角,距离右边缘10像素 + y = 10 # 距离上边缘10像素 + # 绘制文本 + draw.text((x, y), str(self.label), font=font, fill=(255, 0, 0)) # 红色字体 + self.img = ImageTk.PhotoImage(img) + + def move(self, id, cv, target_x, target_y, duration): + start_x, start_y = cv.coords(id) + distance_x = target_x - start_x + distance_y = target_y - start_y + steps = duration // 10 # 以10毫秒为间隔进行移动 + step_x = distance_x / steps + step_y = distance_y / steps + self._move_step(id, cv, start_x, start_y, target_x, target_y, step_x, step_y, steps) + + def _move_step(self,id, cv, start_x, start_y, target_x, target_y, step_x, step_y, steps): + if pause_event.is_set(): + new_x = start_x + step_x + new_y = start_y + step_y + sleep(2) + self._move_step(id, cv, new_x, new_y, target_x, target_y, step_x, step_y, steps) + if steps > 0: + new_x = start_x + step_x + new_y = start_y + step_y + cv.coords(id, new_x, new_y) + cv.update() # 更新画布显示 + sleep(0.01) # 添加延迟以控制动画速度 + self._move_step(id, cv, new_x, new_y, target_x, target_y, step_x, step_y, steps - 1) + else: + cv.coords(id, target_x, target_y) + cv.delete(id) + + def transfer_packet(self, cv: Canvas, nodex_tag: SimBase, nodey_tag: SimBase): + self.objs.add(nodex_tag) + self.objs.add(nodey_tag) + cv.create_image(nodex_tag.ObjX - 15, nodex_tag.ObjY - 15, image=self.img, anchor="nw", tags=self.id) + self.move(self.id, cv, nodey_tag.ObjX - 15, nodey_tag.ObjY - 15, 500) + + +class AllSimConnect(): + # todo: 连接类 + def __init__(self, canvas, nodex: SimBase, nodex_ifs, nodey: SimBase, nodey_ifs, config=None): + """ + 连接对象 + :param nodex: 节点 + :param nodex_ifs: 节点接口 + :param nodey: 节点 + :param nodey_ifs: 节点接口 + """ + self.canvas = canvas + self.ConfigCorrect = 0 if config is None else config + self.NobjS = nodex + self.NobjE = nodey + self.IfsS = nodex_ifs + self.IfsE = nodey_ifs + self.width = False if self.ConfigCorrect == 0 else True # 线的粗细 + + def transfer(self, source_node, packet: SimPacket): + """ + 传输数据 + :param packet: 数据包 + :return: + """ + if source_node == self.NobjS: + packet.transfer_packet(self.canvas, self.NobjS, self.NobjE) + self.NobjE.receive(packet) + else: + packet.transfer_packet(self.canvas, self.NobjE, self.NobjS) + self.NobjS.receive(packet) + + + def draw_line(self): + if self.width: + line = self.canvas.create_line(self.NobjS.ObjX, self.NobjS.ObjY, self.NobjE.ObjX, self.NobjE.ObjY, + width=2, fill="#c372f0", tags=self.NobjS.ObjID + self.NobjE.ObjID + "line") + else: + line = self.canvas.create_line(self.NobjS.ObjX, self.NobjS.ObjY, self.NobjE.ObjX, self.NobjE.ObjY, + width=2, dash=(250, 4), fill="#c372f0", + tags=self.NobjS.ObjID + self.NobjE.ObjID + "line") + self.canvas.tag_lower(line, 2) + + +class SimHost(SimBase): + # todo: 主机类 + """ + 主机类 + """ + + def __init__(self, canvas, x, y, id=None, config=None, label=None): + self.ObjID = str(uuid4()) if id is None else id + super().__init__(canvas, x, y, self.ObjID, config, label) + self.ObjType = 1 + self.ObjLabel = label if label is not None else self.set_default_name() + self.interface = [{}] + self.connections = [None] + self.set_default_config() + + def packet_ok(self, receiveObj, label, size): + count = split_appdata(int(size)) + for i in range(2, count + 1): + pack = SimPacket(self.interface[0]["ip"], self.interface[0]["mac"], + receiveObj.interface[0]["ip"], receiveObj.interface[0]["mac"], label, i, size, self, + receiveObj) + threading.Timer(1 * (i - 1), function=self.send, args=(pack,), ).start() + + def create_packet(self, receiveObj, label, size): + """ + 创建数据包 + :param receiveObj: 目的主机对象 + :param message: 消息 + :return: + """ + def send(message): + print(message) + pack = SimPacket(self.interface[0]["ip"], self.interface[0]["mac"], + receiveObj.interface[0]["ip"], receiveObj.interface[0]["mac"], message, 1, size, self, + receiveObj) + self.send(pack) + message = """发送主机:{}\n\nIP地址:{}\n\nMac地址:{}\n\n发送数据包大小:{}\n\n已发送数据数量:{}\n\n需要发送的数据包总数:{}""" + self.packet_window = PacketWindow(self.canvas, packet={"packet": True, + "size": int(size), + "sendObj": self, + "receiveObj": receiveObj, + "tag": label, + "send": send, + "mode": [{"app": True},{"trans": True},{"ip": True},{"mac": True}]}) + self.packet_window.title("{} 封包".format(self.ObjLabel)) + + def send(self, packet): + """ + 发送数据包 + :param packet: + :return: + """ + connection: AllSimConnect = self.connections[0] + print(f"数据包从 {self.ObjLabel} 发出") + packet.up_jump = connection + connection.transfer(self, packet) + + def receive(self, packet: SimPacket): + """ + 接收数据 + :param packet: 数据包 + :return: + """ + data = "" + def receive(message): + data = message + self.is_un_packet = True + packet.sendObj.packet_ok(self, data, int(packet.size)) + if not self.is_un_packet: + PacketWindow(self.canvas, {"packet": False, + "size": None, + "sendObj": packet.sendObj, + "receiveObj": self, + "tag": packet.message, + "send": receive, + "mode": [{"app": True},{"trans": True},{"ip": True},{"mac": True}]}, packet_step=["app", "trans", "ip", "mac"][::-1]) + size = int(packet.size) + message = """接收主机:{}\n\nIP地址:{}\n\nMac地址:{}\n\n发送数据包大小:{}\n\n已接收数据数量:{}\n\n需要发送的数据包总数:{}""" + self.canvas.show_bottom_message(self, message=message.format(self.ObjLabel, self.interface[0]["ip"], + self.interface[0]["mac"], + size, packet.label, split_appdata(size))) + if split_appdata(size) == packet.label: + if packet.destination_ip == self.interface[0]["ip"]: + self.transfer_animate(True, packet.message, size) + self.canvas.message.show_message(f"{self.ObjLabel}接收到数据:{data}") + else: + self.transfer_animate(False, data, size) + self.canvas.trans_over(packet.objs) + + + def __str__(self): + str = "" + config = self.get_config() + for index, data in config.iterrows(): + str += f"【接口{data['node_ifs']}】\n" + str += f"AppAddr: /Root\n" + str += f"PORT: {data['conn_port']}\n" + str += f"IP: {data['ip']}\n" + str += f"MAC: {data['mac']}\n" + return str + + +class SimRouter(SimBase): + # todo: 路由类 + """ + 路由类 + """ + def __init__(self, canvas, x, y, id=None, config=None, label=None, *args): + self.ObjID = str(uuid4()) if id is None else id + super().__init__(canvas, x, y, self.ObjID, config, label) + self.ObjType = 2 + self.ObjLabel = label if label is not None else self.set_default_name() + self.router_table = {} + self.set_default_router_table() + + def set_default_router_table(self): + """ + 将数据库中的路由表信息提取 + :return: + """ + sql = f"select * from router_table where obj_id='{self.ObjID}'" + router_tables = search(sql) + for index, router_table in router_tables.iterrows(): + if router_table["node_ifs"] in self.router_table: + self.router_table[router_table["node_ifs"]].append(router_table["segment"]) + else: + self.router_table[router_table["node_ifs"]] = [router_table["segment"]] + + def check_destination_ip(self, destination_ip, network): + """ + 检查目标ip是否属于网段范围内 + :param destination_ip: 目标ip + :param network: 网段 + :return:10.2.3.0/24 + """ + ip = ipaddress.ip_address(destination_ip) + network = ipaddress.ip_network(network) + if ip in network: + return True + if network == "0.0.0.0/24": # 如果网段为 0.0.0.0/24 则为默认路由 + return True + + def transmit(self, packet: SimPacket): + """ + 转发数据包 + :return: + """ + def transfer(): + flag = False + next_hop_ifs = None + for conn in self.connections: + if isinstance(conn, AllSimConnect): + if conn.ConfigCorrect == 0: + continue + if conn == packet.up_jump: + continue + ifs = self.connections.index(conn) + 1 + if self.router_table.get(ifs) is None: + continue + for network in self.router_table.get(ifs): + if self.check_destination_ip(packet.destination_ip, network): + flag = True + next_hop_ifs = ifs + if flag: + conn = self.connections[next_hop_ifs - 1] + packet.up_jump = conn + conn.transfer(self, packet) + else: + for conn in self.connections: + if isinstance(conn, AllSimConnect): + if conn == packet.up_jump: + continue + if conn.NobjS != self: + if conn.NobjE.ObjType == 1: + conn.transfer(self, packet) + break + error_message = "路由寻址失败" + self.transfer_animate(False, packet, error_message) + def receive(message): + packet.message = message + self.is_packet = True + transfer() + if not self.is_packet: + PacketWindow(self.canvas, {"packet": True, + "size": None, + "sendObj": packet.sendObj, + "receiveObj": packet.receiveObj, + "tag": packet.message, + "send": receive, + "mode": [{"app": False}, {"trans": False}, {"ip": True}, {"mac": True}]}, + packet_step=["ip", "mac"]) + else: + transfer() + + def receive(self, packet): + """ + 接收数据 + :param packet: 数据包 + :return: + """ + def receive(message): + print(message) + self.is_un_packet = True + packet.message = message + sleep(1.5) + self.transmit(packet) + if not self.is_un_packet: + PacketWindow(self.canvas, {"packet": False, + "size": None, + "sendObj": packet.sendObj, + "receiveObj": packet.receiveObj, + "tag": packet.message, + "send": receive, + "mode": [{"app": False}, {"trans": False}, {"ip": True}, {"mac": True}]}, + packet_step=["ip", "mac"][::-1]) + else: + self.transmit(packet) + + +class SimSwitch(SimBase): + # todo: 交换机类 + """ + 交换机类 + """ + + def __init__(self, canvas, x, y, id=None, config=None, label=None, *args): + self.ObjID = str(uuid4()) if id is None else id + super().__init__(canvas, x, y, self.ObjID, config, label) + self.ObjType = 3 + self.ObjLabel = label if label is not None else self.set_default_name() + self.mac_table = {} + self.set_default_mac_table() + + def set_default_mac_table(self): + """ + 将数据库中的路由表信息提取 + :return: + """ + sql = f"select * from mac_table where obj_id='{self.ObjID}'" + router_tables = search(sql) + for index, router_table in router_tables.iterrows(): + if router_table["node_ifs"] in self.mac_table: + self.mac_table[router_table["node_ifs"]].append(router_table["mac"]) + else: + self.mac_table[router_table["node_ifs"]] = [router_table["mac"]] + + def transmit(self, packet: SimPacket): + """ + 转发数据包 + :return: + """ + flag = False + next_hub_ifs = None + for conn in self.connections: + if isinstance(conn, AllSimConnect): + if conn.ConfigCorrect == 0: + continue + ifs = self.connections.index(conn) + 1 + if packet.destination_mac in self.mac_table.get(ifs, []): + flag = True + next_hub_ifs = ifs + if flag: + conn = self.connections[next_hub_ifs - 1] + packet.up_jump = conn + conn.transfer(self, packet) + return + for conn in self.connections: # 将数据包往所有接口进行转发 + if isinstance(conn, AllSimConnect): + if conn == packet.up_jump: + continue + if conn.ConfigCorrect == 0: + continue + new_packet = SimPacket(packet.source_ip, + packet.source_mac, + packet.destination_ip, + packet.destination_mac, + packet.message) + new_packet.up_jump = conn + threading.Thread(target=conn.transfer, args=(self, new_packet)).start() + + def receive(self, packet: SimPacket): + """ + 接收数据 + :param packet: 数据包 + :return: + """ + print(f"交换机{self.ObjLabel}接受到数据{packet.message}") + self.transmit(packet) + + +class SimHub(SimBase): + """ + 集线器类 + """ + + def __init__(self, canvas, x, y, id=None, config=None, label=None, *args): + self.ObjID = str(uuid4()) if id is None else id + super().__init__(canvas, x, y, self.ObjID, config, label) + self.ObjType = 4 + self.ObjLabel = label if label is not None else self.set_default_name() + + def transmit(self, packet: SimPacket): + """ + 集线器转发数据包 + :return: + """ + for conn in self.connections: # 将数据包往所有接口进行转发 + if isinstance(conn, AllSimConnect): + if conn == packet.up_jump: + continue + if conn.ConfigCorrect == 0: + continue + new_packet = SimPacket(packet.source_ip, + packet.source_mac, + packet.destination_ip, + packet.destination_mac, + packet.message) + new_packet.up_jump = conn + threading.Thread(target=conn.transfer, args=(self, new_packet)).start() + + def receive(self, packet: SimPacket): + """ + 接收数据 + :param packet: 数据包 + :return: + """ + print(f"集线器-{self.ObjLabel}接受到数据,将进行转发!") + self.transmit(packet) diff --git a/new/SimUtil.py b/new/SimUtil.py new file mode 100644 index 0000000..38571d2 --- /dev/null +++ b/new/SimUtil.py @@ -0,0 +1,268 @@ +import ipaddress +import re +import sqlite3 +from tkinter import messagebox + +import ttkbootstrap as tk +from ttkbootstrap import * +from ttkbootstrap import ttk +import pandas as pd + +from SimObjs import SimRouter + + +def round_rectangle(cv, x1, y1, x2, y2, radius=30, **kwargs): + """ + 绘制圆角矩形 + :param cv: canvas对象 + :param radius: 圆角值 + :return: + """ + points = [x1 + radius, y1, + x1 + radius, y1, + x2 - radius, y1, + x2 - radius, y1, + x2, y1, + x2, y1 + radius, + x2, y1 + radius, + x2, y2 - radius, + x2, y2 - radius, + x2, y2, + x2 - radius, y2, + x2 - radius, y2, + x1 + radius, y2, + x1 + radius, y2, + x1, y2, + x1, y2 - radius, + x1, y2 - radius, + x1, y1 + radius, + x1, y1 + radius, + x1, y1] + + return cv.create_polygon(points, **kwargs, smooth=True) + +def validate_ip_address(ip_address): + """ + 匹配ip地址格式是否规范 + :param ip_address: IP地址 + :return: Boolean + """ + # 定义IP地址的正则表达式模式 + pattern_with_subnet = r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})$' + pattern_without_subnet = r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$' + # 使用re模块进行匹配 + match_with_subnet = re.match(pattern_with_subnet, ip_address) + match_without_subnet = re.match(pattern_without_subnet, ip_address) + if match_with_subnet: + # 带有子网掩码的IP地址 + # 检查每个组件的取值范围是否在 0-255 之间 + for group in match_with_subnet.groups()[:4]: + if not (0 <= int(group) <= 255): + return False + # 检查子网掩码的取值范围是否在 0-32 之间 + subnet_mask = int(match_with_subnet.groups()[4]) + if not (0 <= subnet_mask <= 32): + return False + return True + elif match_without_subnet: + # 不带子网掩码的IP地址 + # 检查每个组件的取值范围是否在 0-255 之间 + for group in match_without_subnet.groups(): + if not (0 <= int(group) <= 255): + return False + return True + else: + # IP地址格式不正确 + return False + + +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) + + def import_data(self): + excel_file = pd.ExcelFile(self.path) + sheet_names = excel_file.sheet_names + print(sheet_names) + cursor = self.conn.cursor() + for sheet_name in sheet_names: + # 4. 使用 Pandas 读取工作表数据 + df = pd.read_excel(excel_file, sheet_name=sheet_name) + # 5. 获取工作表的列名 + columns = df.columns.tolist() + # 6. 构造插入语句 + columns_str = ', '.join(columns) + placeholders = ', '.join(['?' for _ in range(len(columns))]) + sql = f"INSERT INTO {sheet_name} ({columns_str}) VALUES ({placeholders})" + # 7. 将数据插入数据库 + for index, row in df.iterrows(): + # 8. 使用动态生成的 SQL 语句将数据插入数据库 + cursor.execute(sql, tuple(row)) + self.conn.commit() + + +class RouterConfigWindow(tk.Toplevel): + def __init__(self, parent, router_obj): + super().__init__(parent) + self.geometry("435x433+350+200") + self.title(f"{router_obj.ObjLabel}路由表配置") + self.router_obj = router_obj + self.interface_entries = [] + self.router_table = {} + self.create_interface_inputs() + self.create_router_table() + + def create_interface_inputs(self): + label_text = ["接口1", "接口2", "接口3", "接口4"] + for i in range(4): + label = tk.Label(self, text=label_text[i], font=("黑体", 16)) + label.grid(row=i, column=0, padx=10, pady=5, sticky="w") + entry = tk.Entry(self, width=20, font=("黑体", 16),) + entry.grid(row=i, column=1, padx=10, pady=5, sticky="w") + self.interface_entries.append(entry) + button = tk.Button(self, text="添加", font=("黑体", 16), command=lambda index=i: self.add_router_entry(index)) + button.grid(row=i, column=2, padx=10, pady=5) + lab = LabelFrame(self, text="示例") + lab.grid(row=4, column=0, columnspan=3, sticky=W, padx=20) + Label(lab, text="10.1.2.0/24 或者 10.1.2.12" if self.router_obj.ObjType == 2 else "MAC11", font=("黑体", 16)).pack() + + def create_router_table(self): + def on_right_click(event): + row = self.router_treeview.identify_row(event.y) # 获取鼠标位置的行索引 + if row: + self.router_treeview.selection_set(row) # 选中该行 + delete_menu.post(event.x_root, event.y_root) # 在鼠标位置弹出删除菜单 + + def delete_row(): + selected_items = self.router_treeview.selection() # 获取选中的行 + for item in selected_items: + ifs, network = int(self.router_treeview.item(item)["values"][0][-1:]), self.router_treeview.item(item)["values"][1] + self.router_obj.delete_config(ifs, network) + self.router_treeview.delete(item) + self.router_table_frame = tk.Frame(self) + self.router_table_frame.grid(row=5, column=0, columnspan=3, padx=10, pady=5) + style = ttk.Style() + style.configure("Custom.Treeview.Heading", font=("宋体", 15)) + style.configure("Custom.Treeview", rowheight=30, font=("宋体", 15)) + self.router_treeview = ttk.Treeview(self.router_table_frame, style="Custom.Treeview", columns=("Interface", "Route"), show="headings") + self.router_treeview.heading("Interface", text="接口") + self.router_treeview.heading("Route", text="网段") + self.router_treeview.pack(side="left", fill="both") + scrollbar = ttk.Scrollbar(self.router_table_frame, orient="vertical", command=self.router_treeview.yview) + scrollbar.pack(side="right", fill="y") + self.router_treeview.configure(yscrollcommand=scrollbar.set) + self.router_table = self.router_obj.router_table + self.router_treeview.bind("", on_right_click) + # 创建删除菜单 + delete_menu = tk.Menu(self.master, tearoff=False) + delete_menu.add_command(label="删除", command=delete_row) + self.update_router_table() + + def add_router_entry(self, index): + entry_text = self.interface_entries[index].get() + try: + ipaddress.ip_network(entry_text) + if isinstance(self.router_obj, SimRouter): + if not validate_ip_address(entry_text): + messagebox.showerror("注意", message="添加的网段信息格式不合格") + self.interface_entries[index].delete(0, tk.END) + self.focus_set() + return + if entry_text: + if index + 1 in self.router_table: + self.router_table[index + 1].append(entry_text) + else: + self.router_table[index + 1] = [entry_text] + self.interface_entries[index].delete(0, tk.END) + self.router_obj.add_config(entry_text, index + 1) + self.update_router_table() + self.master.message.show_message(f"{self.router_obj.ObjLabel}添加配置{entry_text}成功!") + except: + messagebox.showerror("注意", message="网段格式错误!网段示例如下:\n10.1.2.0/24\n10.1.2.12") + return + + def update_router_table(self): + self.router_treeview.delete(*self.router_treeview.get_children()) + for i, entrys in self.router_table.items(): + for entry in entrys: + self.router_treeview.insert("", "end", values=(f"接口{i}", entry)) + + +class SwitchConfigWindow(RouterConfigWindow): + def __init__(self, parent, router_obj): + super().__init__(parent, router_obj) + self.geometry("435x433+350+200") + self.title(f"{router_obj.ObjLabel}交换表配置") + self.router_obj = router_obj + self.interface_entries = [] + self.router_table = {} + self.create_interface_inputs() + self.create_router_table() + + def create_router_table(self): + def on_right_click(event): + row = self.router_treeview.identify_row(event.y) # 获取鼠标位置的行索引 + if row: + self.router_treeview.selection_set(row) # 选中该行 + delete_menu.post(event.x_root, event.y_root) # 在鼠标位置弹出删除菜单 + + def delete_row(): + selected_items = self.router_treeview.selection() # 获取选中的行 + for item in selected_items: + ifs, network = int(self.router_treeview.item(item)["values"][0][-1:]), self.router_treeview.item(item)["values"][1] + self.router_obj.delete_config(ifs, network) + self.router_treeview.delete(item) + + self.router_table_frame = tk.Frame(self) + self.router_table_frame.grid(row=5, column=0, columnspan=3, padx=10, pady=5) + self.router_treeview = ttk.Treeview(self.router_table_frame, columns=("Interface", "Route"), show="headings") + self.router_treeview.heading("Interface", text="接口") + self.router_treeview.heading("Route", text="mac") + self.router_treeview.pack(side="left", fill="both") + scrollbar = ttk.Scrollbar(self.router_table_frame, orient="vertical", command=self.router_treeview.yview) + scrollbar.pack(side="right", fill="y") + self.router_treeview.configure(yscrollcommand=scrollbar.set) + self.router_table = self.router_obj.mac_table + self.router_treeview.bind("", on_right_click) + # 创建删除菜单 + delete_menu = tk.Menu(self.master, tearoff=False) + delete_menu.add_command(label="删除", command=delete_row) + self.update_router_table() + + + def add_router_entry(self, index): + entry_text = self.interface_entries[index].get() + if isinstance(self.router_obj, SimRouter): + if not validate_ip_address(entry_text): + messagebox.showerror("注意", message="添加的网段信息格式不合格") + self.interface_entries[index].delete(0, tk.END) + self.focus_set() + return + if entry_text: + if index + 1 in self.router_table: + self.router_table[index + 1].append(entry_text) + else: + self.router_table[index + 1] = [entry_text] + self.interface_entries[index].delete(0, tk.END) + self.router_obj.add_config(entry_text, index + 1) + self.master.message.show_message(f"{self.router_obj.ObjLabel}添加配置{entry_text}成功!") + self.update_router_table() \ No newline at end of file diff --git a/new/__pycache__/AgreementUtil.cpython-37.pyc b/new/__pycache__/AgreementUtil.cpython-37.pyc new file mode 100644 index 0000000..ab3c443 Binary files /dev/null and b/new/__pycache__/AgreementUtil.cpython-37.pyc differ diff --git a/new/__pycache__/SimObjs.cpython-37.pyc b/new/__pycache__/SimObjs.cpython-37.pyc new file mode 100644 index 0000000..5f93042 Binary files /dev/null and b/new/__pycache__/SimObjs.cpython-37.pyc differ diff --git a/new/__pycache__/SimUtil.cpython-37.pyc b/new/__pycache__/SimUtil.cpython-37.pyc new file mode 100644 index 0000000..0677b30 Binary files /dev/null and b/new/__pycache__/SimUtil.cpython-37.pyc differ diff --git a/new/__pycache__/dbUtil.cpython-37.pyc b/new/__pycache__/dbUtil.cpython-37.pyc new file mode 100644 index 0000000..d110219 Binary files /dev/null and b/new/__pycache__/dbUtil.cpython-37.pyc differ diff --git a/new/__pycache__/packet.cpython-37.pyc b/new/__pycache__/packet.cpython-37.pyc new file mode 100644 index 0000000..8afc343 Binary files /dev/null and b/new/__pycache__/packet.cpython-37.pyc differ diff --git a/new/database.xlsx b/new/database.xlsx new file mode 100644 index 0000000..1f560c6 Binary files /dev/null and b/new/database.xlsx differ diff --git a/new/dbUtil.py b/new/dbUtil.py new file mode 100644 index 0000000..f2e20ef --- /dev/null +++ b/new/dbUtil.py @@ -0,0 +1,104 @@ +import sqlite3 +import sys + +import pandas as pd +from pandas import DataFrame + +conn = sqlite3.connect(sys.path[0]+"/network.db") + +def execute_sql(sql): + """ + 执行sql语句 + :param sql: + :return: + """ + cursor = conn.cursor() + cursor.execute(sql) + conn.commit() + + +def search(sql) -> DataFrame: + return pd.read_sql(sql, conn) + + +def delete_obj(obj_id): + cursor = conn.cursor() + delete_obj_sql = f"delete from sim_objs where ObjID='{obj_id}'" + cursor.execute(delete_obj_sql) + delete_conn_sql = f"delete from sim_conn where conn_id in (select conn_id from conn_config where node_id='{obj_id}')" + cursor.execute(delete_conn_sql) + conn.commit() + + +def truncate_db(): + init_database() + +def init_database(): + cursor = conn.cursor() + cursor.execute(""" + DROP TABLE IF EXISTS `conn_config`; + """) + cursor.execute(""" + CREATE TABLE `conn_config` ( + `conn_id` varchar(55) NULL DEFAULT NULL, + `node_id` varchar(55) NULL DEFAULT NULL, + `node_ifs` int(0) NULL DEFAULT NULL, + `ip` varchar(55) NULL DEFAULT NULL, + `mac` varchar(128) NULL DEFAULT NULL, + `conn_port` varchar(32) NULL DEFAULT NULL, + `addr` varchar(255) NULL DEFAULT NULL, + CONSTRAINT `conn_config_sim_conn_conn_id_fk` FOREIGN KEY (`conn_id`) REFERENCES `sim_conn` (`conn_id`) ON DELETE CASCADE ON UPDATE CASCADE +) ; + """) + cursor.execute(""" + DROP TABLE IF EXISTS `mac_table`; + """) + cursor.execute(""" + CREATE TABLE `mac_table` ( + `obj_id` varchar(55) NULL DEFAULT NULL, + `node_ifs` int(0) NULL DEFAULT NULL, + `mac` varchar(55) NULL DEFAULT NULL, + CONSTRAINT `mac_table_sim_objs_ObjID_fk` FOREIGN KEY (`obj_id`) REFERENCES `sim_objs` (`ObjID`) ON DELETE CASCADE ON UPDATE CASCADE +) ; + """) + cursor.execute(""" + DROP TABLE IF EXISTS `router_table`; + """) + cursor.execute(""" + CREATE TABLE `router_table` ( + `obj_id` varchar(55) NULL DEFAULT NULL, + `node_ifs` int(0) NULL DEFAULT NULL, + `segment` varchar(55) NULL DEFAULT NULL, + CONSTRAINT `router_table_sim_objs_ObjID_fk` FOREIGN KEY (`obj_id`) REFERENCES `sim_objs` (`ObjID`) ON DELETE CASCADE ON UPDATE CASCADE +) ; + + """) + cursor.execute(""" + DROP TABLE IF EXISTS `sim_conn`; + """) + cursor.execute(""" + CREATE TABLE `sim_conn` ( + `conn_id` varchar(255) NOT NULL , + `ConfigCorrect` int(0) NULL DEFAULT NULL , + PRIMARY KEY (`conn_id`) +) ; + + """) + cursor.execute(""" + DROP TABLE IF EXISTS `sim_objs`; + """) + cursor.execute(""" + CREATE TABLE `sim_objs` ( + `ObjID` varchar(50) NOT NULL, + `ObjType` int(0) NULL DEFAULT NULL, + `ObjLabel` varchar(20) NULL DEFAULT NULL, + `ObjX` int(0) NULL DEFAULT NULL, + `ObjY` int(0) NULL DEFAULT NULL, + `ConfigCorrect` int(0) NULL DEFAULT NULL, + PRIMARY KEY (`ObjID`) +) ; + """) + conn.commit() + +if __name__ == '__main__': + init_database() diff --git a/new/network.db b/new/network.db new file mode 100644 index 0000000..2b5d9a0 Binary files /dev/null and b/new/network.db differ diff --git a/new/packet.py b/new/packet.py new file mode 100644 index 0000000..f50e26f --- /dev/null +++ b/new/packet.py @@ -0,0 +1,438 @@ +import time + + +from SimObjs import * +from tkinter import * +from tkinter import messagebox + +from PIL import ImageTk, Image +from AgreementUtil import AgreementUtil + + +def create_label(canvas: Canvas, x, y, width, height, text, rect_color, font_color, tag): + canvas.create_rectangle(x, y, x + width, y + height, fill=rect_color, tags=tag) + canvas.create_text(x + width / 2, y + height / 2, text=text, fill=font_color, tags=tag) + return tag + + +class PacketWindow(Toplevel): + def __init__(self, master, packet, packet_step=["app", "trans", "ip", "mac"], *args, **kwargs): + """ + packet:{"packet": True, "mode": {"app": True, "trans": True, "ip": True, "mac": True}} + """ + super().__init__(master, *args, **kwargs) + self.master = master + self.width = 300 + self.height = 400 + self.geometry(f"{self.width}x{self.height}+200+200") + self.attributes("-topmost", True) + self.resizable(width=False, height=False) + self.packet_button_str = {"app": "应用层", "ip": "网络层", "mac": "链路层", "trans": "传输层"} + if not packet["packet"]: + packet["mode"] = packet["mode"][::-1] # True 为封包 False 为解包 + self.packet_step = packet_step + self.step = 0 + self.packet: dict[str: bool] = packet + self.sendObj: SimHost = packet["sendObj"] + self.receiveObj: SimHost = packet["receiveObj"] + self.packet_option = {"app": {"color": "#ff3b26", "command": self.app_packet if packet["packet"] else self.app_unpack}, + "ip": {"color": "#00a2f2", "command": self.ip_packet if packet["packet"] else self.ip_unpack}, + "mac": {"color": "#46707a", "command": self.mac_packet if packet["packet"] else self.mac_unpack}, + "trans": {"color": "#710a00", "command": self.trans_packet if packet["packet"] else self.trans_unpack}} + self.canvas = Canvas(self, width=self.width, height=self.height) + self.canvas.place(x=0, y=0, anchor='nw') + self.packet_height, self.packet_init_width = 25, 100 + self.background_img = ImageTk.PhotoImage(Image.open("../datas/images/背景@2x.png").resize((self.width, self.height))) + self.canvas.create_image(0, 0, image=self.background_img, anchor=NW) + # self.packet_rect = create_label(self.canvas, int(self.width / 2) - self.packet_init_width / 2, 10, width=self.packet_init_width, height=self.packet_height, text=self.packet["tag"], rect_color="#dee1e6", font_color="black", tag="packet") + self.step_y = {} + self.message = packet["tag"] + self.create_widget() + + def move_to(self, tag, target_x, target_y, speed=10): + # 获取当前位置 + current_coords = self.canvas.coords(tag) + if len(current_coords) < 2: + return # 如果没有坐标,就退出函数 + current_x, current_y = current_coords[0], current_coords[1] + # 计算移动方向 + delta_x = (target_x if target_x else current_x) - current_x + delta_y = (target_y if target_y else current_y) - current_y + # 计算下一步的位置 + next_x = current_x + (speed if delta_x > 0 else -speed) if abs(delta_x) > speed else (target_x if target_x else current_x) + next_y = current_y + (speed if delta_y > 0 else -speed) if abs(delta_y) > speed else (target_y if target_y else current_y) + # 移动对象 + self.canvas.move(tag, next_x - current_x, next_y - current_y) + # 如果对象还没有到达目标,继续移动 + if next_x != (target_x if target_x else current_x) or next_y != (target_y if target_y else current_y): + self.canvas.after(10, lambda: self.move_to(tag, target_x, target_y, speed)) + + def create_widget(self): + mode_text = "封包" if self.packet["packet"] else "解包" + num, margin_top, button_width, button_height = 1, 30, 120, 40 + for data in self.packet["mode"]: + key = list(data.keys())[0] + value = list(data.values())[0] + if value: + Button(self, text=self.packet_button_str[key] + mode_text, command=self.packet_option[key]["command"], + font=("", 16)).place(x=40, y=30 + (margin_top + button_height) * (num - 1), + width=button_width, height=button_height) + # 动画 + # self.canvas.create_rectangle(self.width - 80 - 40, 80 + (margin_top + 20 + button_height) * (num - 1), + # self.width - 40, 80 + (margin_top + 20 + button_height) * (num - 1) + 25, fill=self.packet_option[key]["color"]) + # self.canvas.create_text(self.width - 40 - 40, 80 + (margin_top + 20 + button_height) * (num - 1) + 12.5, + # text=self.packet_button_str[key], font=("", 16), fill="white") + # self.step_y[key] = 80 + (margin_top + 20 + button_height) * (num - 1) + num += 1 + Button(self, text="发送", command=self.send_packet, font=("", 16)).place(x=self.width - 60, y=self.height - 40, anchor=NW) + Button(self, text="取消", command=self.destroy, font=("", 16)).place(x=self.width - 120, y=self.height - 40, anchor=NW) + + def send_packet(self): + if self.step != len(self.packet_step): + messagebox.showerror("注意", "尚未完成{}!".format("封包" if self.packet["packet"] else "解包")) + return + self.packet["send"](self.message) + self.destroy() + + def create_window(self, option): + toplevel = Toplevel(self) + toplevel.title(option["title"]) + toplevel.geometry("450x220+300+300") + for entry_option in option["entry"]: + index = option["entry"].index(entry_option) + key = list(entry_option.keys())[0] + value = list(entry_option.values())[0] + Label(toplevel, text=key, font=("", 16)).grid(row=index, column=0, padx=20, pady=10) + if value is None: + continue + Entry(toplevel, textvariable=value, font=("", 16), width=15).grid(row=index, column=1, pady=10) + Button(toplevel, text="提交", command=option["command"], font=("", 16)).place(x=450 - 60, y=220 - 40, anchor=NW) + Button(toplevel, text="取消", command=toplevel.destroy, font=("", 16)).place(x=450 - 120, y=220 - 40, anchor=NW) + toplevel.attributes("-topmost", True) + return toplevel + + def app_packet(self): + """ + 应用层封包 + """ + if self.packet_step[self.step] != "app": + messagebox.showerror("注意", "封包顺序出错!") + return + SourceAppAddr = StringVar() + TargetAppAddr = StringVar() + def packet(): + toplevel.destroy() + source_app_addr = SourceAppAddr.get() + target_app_addr = TargetAppAddr.get() + if source_app_addr == self.sendObj.interface[0]["addr"] \ + and target_app_addr == self.receiveObj.interface[0]["addr"]: + + # 动画 + # app_rect = create_label(self.canvas, self.width / 2 + 200, int(self.step_y["app"]), 50, 25, "AH", "#ff3b26", "white", tag="app") + # self.move_to(self.packet_rect, None, int(self.step_y["app"]), speed=1) + # self.move_to(app_rect, self.width / 2 + self.packet_init_width / 2, int(self.step_y["app"]), speed=1) + + self.message = target_app_addr + "&" + self.packet["tag"] + self.master.message.show_message("应用层封包成功!数据包如下: \n{}".format(self.message)) + self.step += 1 + else: + messagebox.showerror("提示", "应用层地址填写有误,请仔细检查!") + + toplevel = self.create_window({"title": "应用层封包", "entry": [{"发送主机应用层地址:": SourceAppAddr}, {"接收主机应用层地址:": TargetAppAddr}], + "command": packet}) + + def app_unpack(self): + """ + 应用层解包 + """ + if self.packet_step[self.step] != "app": + messagebox.showerror("注意", "解包顺序出错!") + return + TargetAppAddr = StringVar() + def packet(): + toplevel.destroy() + target_app_addr = TargetAppAddr.get() + if target_app_addr == self.receiveObj.interface[0]["addr"]: + self.message = self.message.split("&")[1] + self.master.message.show_message("应用层解包成功!数据包如下: \n{}".format(self.message)) + self.step += 1 + else: + messagebox.showerror("提示", "应用层地址填写有误,请仔细检查!") + toplevel = self.create_window({"title": "应用层解包", "entry": [{"接收主机应用层地址:": TargetAppAddr}], + "command": packet}) + + def trans_packet(self): + """ + 传输层封包 + """ + if self.packet_step[self.step] != "trans": + messagebox.showerror("注意", "封包顺序出错!") + return + SentPort = StringVar() + RcptPort = StringVar() + SplitCount = IntVar() + + def packet(): + + sent_port = SentPort.get() + rcpt_port = RcptPort.get() + split_count = SplitCount.get() + count = split_appdata(self.packet["size"]) + print(sent_port, self.sendObj.interface[0]["conn_port"]) + if sent_port == self.sendObj.interface[0]["conn_port"] \ + and rcpt_port == self.receiveObj.interface[0]["conn_port"]\ + and split_count == count: + toplevel.destroy() + # 动画 + # self.canvas.delete(self.packet_rect) + # self.canvas.delete("app") + # self.packet_rect = create_label(self.canvas, int(self.width / 2) - self.packet_init_width / 2, int(self.step_y["app"]), + # width=self.packet_init_width + 50, height=self.packet_height, + # text="A-" + self.packet["tag"], rect_color="#dee1e6", font_color="black", + # tag="packet") + # trans_rect = create_label(self.canvas, self.width / 2 + 200, int(self.step_y["trans"]), 50, 25, "PH", "#ff3b26", + # "white", tag="trans") + # self.move_to(self.packet_rect, None, int(self.step_y["trans"]), speed=1) + # self.move_to(trans_rect, self.width / 2 + self.packet_init_width / 2 + 50, int(self.step_y["trans"]), speed=1) + + + self.message = AgreementUtil.create_udp_packet(self.message, int(sent_port), int(rcpt_port)) + self.master.message.show_message("传输层封包成功!数据包如下:\n{}".format(str([str(i + 1) + "-" + str(self.message) for i in range(split_count)]))) + self.step += 1 + else: + messagebox.showerror("提示", "传输层封包信息填写有误,请仔细检查!") + + toplevel = self.create_window({"title": "传输层封包", "entry": [{"发送主机端口:": SentPort}, + {"接收主机端口:": RcptPort}, + {"拆包数量": SplitCount}, + {"每个包约2048": None}], + "command": packet}) + + def trans_unpack(self): + """ + 传输层解包 + """ + print(self.packet_step) + print(self.step) + if self.packet_step[self.step] != "trans": + messagebox.showerror("注意", "解包顺序出错!") + return + RcptPort = StringVar() + + def packet(): + rcpt_port = RcptPort.get() + if rcpt_port == self.receiveObj.interface[0]["conn_port"]: + toplevel.destroy() + data = AgreementUtil.parse_udp_packet(self.message) + print(data) + self.message = data[-1] + self.master.message.show_message("传输层解包成功!数据包如下:\n{}".format(self.message)) + self.step += 1 + else: + messagebox.showerror("提示", "传输层解包信息填写有误,请仔细检查!") + + toplevel = self.create_window({"title": "传输层解包", "entry": [{"接收主机端口:": RcptPort}], + "command": packet}) + + def ip_packet(self): + """ + 网络层封包 + """ + if self.packet_step[self.step] != "ip": + messagebox.showerror("注意", "封包顺序出错!") + return + SourceIP = StringVar() + TargetIP = StringVar() + def packet(): + source_ip = SourceIP.get() + target_ip = TargetIP.get() + if source_ip == self.sendObj.interface[0]["ip"] \ + and target_ip == self.receiveObj.interface[0]["ip"]: + toplevel.destroy() + self.message = AgreementUtil.create_ip_packet(self.message, source_ip, target_ip) + self.master.message.show_message("网络层封包成功!数据包如下:\n{}".format(self.message)) + self.step += 1 + else: + messagebox.showerror("提示", "网络层封包信息填写有误,请仔细检查!") + toplevel = self.create_window({"title": "网络层封包", "entry": [{"发送主机IP:": SourceIP}, + {"接收主机IP:": TargetIP}], + "command": packet}) + + def ip_unpack(self): + """ + 网络层解包 + """ + print(self.packet_step) + print(self.step) + if self.packet_step[self.step] != "ip": + messagebox.showerror("注意", "解包顺序出错!") + return + RcptIP = StringVar() + + def packet(): + rcpt_ip = RcptIP.get() + if rcpt_ip == self.receiveObj.interface[0]["ip"]: + toplevel.destroy() + data = AgreementUtil.parse_ip_packet(self.message) + print(data) + self.message = data[-1] + self.master.message.show_message("网络层解包成功!数据包如下:\n{}".format(self.message)) + self.step += 1 + else: + messagebox.showerror("提示", "网络层解包信息填写有误,请仔细检查!") + + toplevel = self.create_window({"title": "网络层解包", "entry": [{"接收主机IP:": RcptIP}], + "command": packet}) + + def mac_packet(self): + """ + 链路层封包 + """ + if self.packet_step[self.step] != "mac": + messagebox.showerror("注意", "封包顺序出错!") + return + SentMac = StringVar() + RcptMac = StringVar() + def packet(): + sent_mac = SentMac.get() + rcpt_mac = RcptMac.get() + if sent_mac == self.sendObj.interface[0]["mac"] \ + and rcpt_mac == self.receiveObj.interface[0]["mac"]: + toplevel.destroy() + self.message = AgreementUtil.create_ethernet_frame(self.message, sent_mac, rcpt_mac) + self.master.message.show_message("链路层封包成功!数据包如下:\n{}".format(self.message)) + self.step += 1 + else: + messagebox.showerror("提示", "链路层封包信息填写有误,请仔细检查!") + toplevel = self.create_window({"title": "链路层封包", "entry": [{"发送主机MAC:": SentMac}, + {"接收主机MAC:": RcptMac}], + "command": packet}) + + def mac_unpack(self): + """ + 链路层解包 + """ + if self.packet_step[self.step] != "mac": + messagebox.showerror("注意", "解包顺序出错!") + return + RcptMac = StringVar() + def packet(): + rcpt_mac = RcptMac.get() + if rcpt_mac == self.receiveObj.interface[0]["mac"]: + toplevel.destroy() + data = AgreementUtil.parse_ethernet_frame(self.packet["tag"]) + print(data) + self.message = data[-1] + self.master.message.show_message("链路层解包成功!数据包如下:\n{}".format(self.message)) + self.step += 1 + else: + messagebox.showerror("提示", "链路层解包信息填写有误,请仔细检查!") + toplevel = self.create_window({"title": "链路层解包", "entry": [{"接收主机MAC:": RcptMac}], + "command": packet}) + + +class AppData: + def __init__(self, obj_id, id_key, app_pack_id, app_pack_size, app_pack_tag, source_app_addr, target_app_addr, app_packed_string, timestamp, canvas): + self.obj_id = obj_id + self.id_key = id_key + self.app_pack_id = app_pack_id + self.app_pack_size = app_pack_size + self.app_pack_tag = app_pack_tag + self.source_app_addr = source_app_addr + self.target_app_addr = target_app_addr + self.app_packed_string = app_packed_string + self.timestamp = timestamp + + + def pack(self): + # 为了简化,我们打包成一个字典 + return vars(self) + + @staticmethod + def unpack(packed_data): + # 解包为AppData对象 + return AppData(**packed_data) + +class TransData: + def __init__(self, obj_id, id_key, trans_pack_id, trans_seq, trans_tag, app_pack_id, sent_port, rcpt_port, source_app_addr, target_app_addr, trans_packed_string, timestamp): + self.obj_id = obj_id + self.id_key = id_key + self.trans_pack_id = trans_pack_id + self.trans_seq = trans_seq + self.trans_tag = trans_tag + self.app_pack_id = app_pack_id + self.sent_port = sent_port + self.rcpt_port = rcpt_port + self.source_app_addr = source_app_addr + self.target_app_addr = target_app_addr + self.trans_packed_string = trans_packed_string + self.timestamp = timestamp + + def pack(self): + return vars(self) + + @staticmethod + def unpack(packed_data): + return TransData(**packed_data) + + +class IPData: + def __init__(self, obj_id, id_key, ip_pack_id, trans_pack_id, source_ip, target_ip, source_app_addr, target_app_addr, ip_packed_string): + self.obj_id = obj_id + self.id_key = id_key + self.ip_pack_id = ip_pack_id + self.trans_pack_id = trans_pack_id + self.source_ip = source_ip + self.target_ip = target_ip + self.source_app_addr = source_app_addr + self.target_app_addr = target_app_addr + self.ip_packed_string = ip_packed_string + + def pack(self): + return vars(self) + + @staticmethod + def unpack(packed_data): + return IPData(**packed_data) + + +class MACData: + def __init__(self, obj_id, id_key, mac_pack_id, ip_pack_id, sent_mac, rcpt_mac, source_ip, target_ip, source_app_addr, target_app_addr, mac_packed_string): + self.obj_id = obj_id + self.id_key = id_key + self.mac_pack_id = mac_pack_id + self.ip_pack_id = ip_pack_id + self.sent_mac = sent_mac + self.rcpt_mac = rcpt_mac + self.source_ip = source_ip + self.target_ip = target_ip + self.source_app_addr = source_app_addr + self.target_app_addr = target_app_addr + self.mac_packed_string = mac_packed_string + + def pack(self): + return vars(self) + + @staticmethod + def unpack(packed_data): + return MACData(**packed_data) + + +def split_appdata(AppPackSize): + MTU = 2048 + return AppPackSize // MTU + (AppPackSize % MTU > 0) + + +if __name__ == '__main__': + # 假设的最大传输单元(MTU) + MTU = 2048 # bytes + + # 模拟的数据和数据大小 + SIMULATED_DATA = "hello" + SIMULATED_SIZE = 2049 # 10 MB + + # 创建应用层数据包 + app_packet = AppData("123", "123", "app1", SIMULATED_SIZE, "DATA", "192.0.2.1", "198.51.100.1", SIMULATED_DATA, time.time()) + packed_app_data = app_packet.pack() + print(packed_app_data) + diff --git a/new/random_mac.py b/new/random_mac.py new file mode 100644 index 0000000..a033311 --- /dev/null +++ b/new/random_mac.py @@ -0,0 +1,9 @@ +import random + +def random_mac(): + # 生成六组两位的十六进制数 + return ":".join(["%02x" % random.randint(0, 255) for _ in range(6)]) + +# 生成MAC地址 +mac_address = random_mac() +print(mac_address) diff --git a/new/text b/new/text new file mode 100644 index 0000000..d19a2b3 --- /dev/null +++ b/new/text @@ -0,0 +1,55 @@ +packet_count = split_appdata(int(size)) + + self.canvas.message.show_message("传输层拆包成功!数量:{}".format(packet_count)) + for i in range(1, packet_count + 1): + trans_data = TransData( + obj_id=self.ObjID, + id_key=app_data.id_key, + trans_pack_id=str(i), + trans_seq=i, + trans_tag=label, + app_pack_id=app_data.app_pack_id, + sent_port=int(self.interface[0]["conn_port"].split(".")[0]), + rcpt_port=80, + source_app_addr=self.interface[0]['ip'], + target_app_addr=ip, + trans_packed_string=app_data.pack(), + timestamp=int(time.time()) + ) + self.canvas.message.show_message("传输层数据包{}:{}".format(i, trans_data.pack())) + ip_data = IPData( + obj_id=self.ObjID, + id_key="1001", + ip_pack_id="1001", + trans_pack_id=str(i), + source_ip=self.interface[0]["ip"], + target_ip=ip, + source_app_addr=self.interface[0]["ip"], + target_app_addr=ip, + ip_packed_string=trans_data.pack() + ) + self.canvas.message.show_message("网络层数据包:{}".format(trans_data.pack())) + mac_data = MACData( + obj_id=self.ObjID, + id_key=app_data.id_key, + mac_pack_id="1001", + ip_pack_id=str(i), + sent_mac=self.interface[0]["mac"], + rcpt_mac=mac, + source_ip=self.interface[0]["ip"], + target_ip=ip, + source_app_addr=self.interface[0]["ip"], + target_app_addr=ip, + mac_packed_string=ip_data.pack() + ) + self.canvas.message.show_message("链路层数据包:{}".format(mac_data.pack())) + self.canvas.show_top_message(self, message=message.format(self.ObjLabel, self.interface[0]["ip"], self.interface[0]["mac"], + size, i, packet_count)) + packet = SimPacket(self.interface[0]["ip"], self.interface[0]["mac"], ip, mac, mac_data.pack()) + thread = threading.Timer(i, function=self.send, args=(packet,)) + thread.start() + + tk.Button(toplevel, text='开启模拟', font=('黑体', 16), height=1, command=split_packet).grid(row=5, column=0, sticky='e', + pady=10) + tk.Button(toplevel, text='取消', font=('黑体', 16), height=1, command=toplevel.destroy).grid(row=5, column=1, + sticky='e', pady=10) \ No newline at end of file diff --git a/网络传输.png b/网络传输.png new file mode 100644 index 0000000..a547082 Binary files /dev/null and b/网络传输.png differ