网络路由器查表过程模拟
任务及要求
编程模拟路由器查表过程,用(目的地址 掩码 下一跳)的IP路由表以及目的地址作为输入,为目的地址查找路由表,找出正确的下一跳并输出结果。
1.
建立一个路由表
f=open(\'路由表.txt\',\'r\')
list1=[]
i=0
while(1):
y=f.readline()
if y==\'\':
break
list1.append(y)
f.close()
while(1):
z=input("目的IP地址:")
x=z.split(\'.\')
for i in range(2,len(list1)):
a=list1[i].split()
q=a[1].split(\'.\')
e=a[2].split(\'.\')
if a[0]==\'1\':
if((int(x[0])&int(e[0])==int(q[0])) and (int(x[1])&int(e[1])==int(q[1])) and (int(x[2])&int(e[2])==int(q[2])) and (int(x[3])&int(e[3])==int(q[3]))):
print("直接交付",a[3])
break
else:
if ((int(x[0])&int(e[0])==int(q[0])) and (int(x[1])&int(e[1])==int(q[1])) and (int(x[2])&int(e[2])==int(q[2])) and (int(x[3])&int(e[3])==int(q[3]))):
print("间接交付,下一跳为:",a[3])
break
else:
continue
else:
if ((int(x[0])&int(e[0])==int(q[0])) and (int(x[1])&int(e[1])==int(q[1])) and (int(x[2])&int(e[2])==int(q[2])) and (int(x[3])&int(e[3])==int(q[3]))):
print(a[3])
break
else:
print("转发分组出错!")
break
版权声明:本文为llb123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。