You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
580 B

import socket
def is_same_subnet(ip1, ip2):
# 将IP地址和子网掩码转换为整数列表
ip1_parts = [int(part) for part in ip1.split('.')]
ip2_parts = [int(part) for part in ip2.split('.')]
subnet_mask_parts = [int(part) for part in "255.255.255.0".split('.')]
# 计算网络地址
network1 = [ip1_parts[i] & subnet_mask_parts[i] for i in range(4)]
network2 = [ip2_parts[i] & subnet_mask_parts[i] for i in range(4)]
# 比较网络地址是否相同
return network1 == network2
print(is_same_subnet("192.168.10.23", "192.12.10.21"))