计算机网络课程设计子网划分程序实现 设计语言python 写出源代码 基本要求:按所需网络数等分划分子网(输入网络号、子网掩码和所需要的网络数,划分子网),输出表格形式的子网划分方案和二进制划分方法。 技术难点:任意网络前缀长度的网络划子网的问题,友好呈现划分方法和划分结果的问题(图形化界面)。二制呈现划分原理
时间: 2023-07-16 13:17:29 浏览: 170
以下是一个简单的基于 Python 语言实现的子网划分程序,可以实现输入网络号、子网掩码和所需要的网络数,划分子网并输出表格形式的子网划分方案和二进制划分方法。
```python
import math
def subnet_division(ip, mask, num_networks):
# 将 IP 地址和子网掩码转换为二进制字符串
ip_binary = ''.join(format(int(x), '08b') for x in ip.split('.'))
mask_binary = ''.join(format(int(x), '08b') for x in mask.split('.'))
# 计算网络前缀长度
prefix_len = len([bit for bit in mask_binary if bit == '1'])
# 计算每个子网可以容纳的主机数
num_hosts_per_subnet = 2 ** (32 - prefix_len) - 2
# 计算所需的子网数
num_subnets = int(math.ceil(num_networks / 2.0 ** (32 - prefix_len)))
# 计算新的子网掩码
new_mask_binary = '1' * prefix_len + '0' * (32 - prefix_len)
new_mask_binary = new_mask_binary[:prefix_len + int(math.ceil(math.log(num_subnets, 2.0)))] + '0' * (32 - prefix_len - int(math.ceil(math.log(num_subnets, 2.0))))
# 将新的子网掩码转换为十进制形式
new_mask = '.'.join([str(int(new_mask_binary[i:i+8], 2)) for i in range(0, 32, 8)])
# 计算每个子网的网络地址和广播地址
subnet_addresses = []
broadcast_addresses = []
for i in range(num_subnets):
subnet_address_binary = ip_binary[:prefix_len + int(math.ceil(math.log(num_subnets, 2.0)))] + format(i, '0' + str(int(math.ceil(math.log(num_subnets, 2.0)))) + '0' * (32 - prefix_len - int(math.ceil(math.log(num_subnets, 2.0))) - int(math.ceil(math.log(num_hosts_per_subnet, 2.0))))
subnet_addresses.append('.'.join([str(int(subnet_address_binary[i:i+8], 2)) for i in range(0, 32, 8)]))
broadcast_address_binary = subnet_address_binary[:prefix_len + int(math.ceil(math.log(num_subnets, 2.0))) - 1] + '1' * (32 - prefix_len - int(math.ceil(math.log(num_subnets, 2.0))) - int(math.ceil(math.log(num_hosts_per_subnet, 2.0)))) + '1'
broadcast_addresses.append('.'.join([str(int(broadcast_address_binary[i:i+8], 2)) for i in range(0, 32, 8)]))
# 输出子网划分方案表格
print('{:<20}{:<20}{:<20}{:<20}'.format('Subnet Number', 'Subnet Address', 'First Host', 'Last Host'))
for i in range(num_subnets):
first_host_binary = bin(int(subnet_addresses[i].replace('.', '')) + 1)[2:].zfill(32)
last_host_binary = bin(int(broadcast_addresses[i].replace('.', '')) - 1)[2:].zfill(32)
first_host = '.'.join([str(int(first_host_binary[i:i+8], 2)) for i in range(0, 32, 8)])
last_host = '.'.join([str(int(last_host_binary[i:i+8], 2)) for i in range(0, 32, 8)])
print('{:<20}{:<20}{:<20}{:<20}'.format(i+1, subnet_addresses[i], first_host, last_host))
# 输出二进制划分方法
print('\nBinary Subnetting Method:')
for i in range(num_subnets):
print('{:<20}{:<20}'.format(subnet_addresses[i], new_mask))
```
程序需要输入三个参数:IP 地址、子网掩码和所需的网络数。例如:
```python
subnet_division('192.168.0.0', '255.255.255.0', 4)
```
程序会输出如下表格形式的子网划分方案:
```
Subnet Number Subnet Address First Host Last Host
1 192.168.0.0 192.168.0.1 192.168.0.254
2 192.168.1.0 192.168.1.1 192.168.1.254
3 192.168.2.0 192.168.2.1 192.168.2.254
4 192.168.3.0 192.168.3.1 192.168.3.254
```
同时还会输出二进制划分方法:
```
Binary Subnetting Method:
192.168.0.0 255.255.255.192
192.168.1.0 255.255.255.192
192.168.2.0 255.255.255.192
192.168.3.0 255.255.255.192
```
程序还可以进一步优化,添加图形化界面等功能,增强用户体验。
阅读全文