-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3059b1
commit 0ea0792
Showing
11 changed files
with
128 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- mode: python ; coding: utf-8 -*- | ||
|
||
|
||
a = Analysis( | ||
['C:\\Users\\胡成龙\\Desktop\\Network-Calculator\\main.py'], | ||
pathex=[], | ||
binaries=[], | ||
datas=[('C:\\Users\\胡成龙\\Desktop\\Network-Calculator\\docs', 'docs'), ('C:\\Users\\胡成龙\\Desktop\\Network-Calculator\\images', 'images')], | ||
hiddenimports=[], | ||
hookspath=[], | ||
hooksconfig={}, | ||
runtime_hooks=[], | ||
excludes=[], | ||
noarchive=False, | ||
optimize=0, | ||
) | ||
pyz = PYZ(a.pure) | ||
|
||
exe = EXE( | ||
pyz, | ||
a.scripts, | ||
a.binaries, | ||
a.datas, | ||
[], | ||
name='IP地址规划计算器v1.3.1', | ||
debug=False, | ||
bootloader_ignore_signals=False, | ||
strip=False, | ||
upx=True, | ||
upx_exclude=[], | ||
runtime_tmpdir=None, | ||
console=False, | ||
disable_windowed_traceback=False, | ||
argv_emulation=False, | ||
target_arch=None, | ||
codesign_identity=None, | ||
entitlements_file=None, | ||
icon=['C:\\Users\\胡成龙\\Desktop\\Network-Calculator\\images\\logo.ico'], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,71 @@ | ||
# network_class_utils.py | ||
import re | ||
import ipaddress | ||
|
||
# 根据CIDR位数判断网络类别 | ||
# 根据CIDR位数和地址判断网络类别 | ||
def get_network_class_by_cidr(cidr): | ||
print(cidr) | ||
# 验证CIDR格式 | ||
validation_result = validate_cidr(cidr) | ||
print(f"CIDR验证结果: {validation_result}") # 打印验证结果 | ||
if validation_result != "valid": | ||
return validation_result | ||
|
||
# 如果CIDR只给了位数(如 /24),则假设一个默认IP地址 | ||
if not cidr.startswith('192.168.1.0'): | ||
cidr = f"192.168.1.0{cidr}" | ||
|
||
# 提取CIDR中的子网掩码部分(即位数) | ||
cidr_bits = int(cidr.split('/')[1]) | ||
|
||
# 判断网络类别 | ||
if 0 <= cidr_bits <= 7: | ||
return "Any" # /7以下 | ||
elif 8 <= cidr_bits <= 15: | ||
return "A类" # /8 到 /15 | ||
elif 16 <= cidr_bits <= 23: | ||
return "B类" # /16 到 /23 | ||
elif 24 <= cidr_bits <= 32: | ||
return "C类" # /24 到 /32 | ||
# 处理CIDR格式,确保IP是网络地址而不是主机地址 | ||
if '/' in cidr: | ||
ip, prefix = cidr.split('/') | ||
print(f"IP地址: {ip}, 子网掩码位数: {prefix}") # 打印拆分后的IP地址和子网掩码位数 | ||
network = ipaddress.IPv4Network(f"{ip}/{prefix}", strict=False) | ||
else: | ||
return "无效的CIDR" | ||
print(f"没有子网掩码,默认使用 /16 位数") | ||
network = ipaddress.IPv4Network(f"{cidr}/16", strict=False) # 默认 /16 | ||
|
||
# 输出网络地址信息 | ||
print(f"网络地址: {network.network_address}, 网络范围: {network}") # 打印网络地址 | ||
|
||
# 判断IP地址所在类别 | ||
ip = str(network.network_address) | ||
print(f"判断的IP: {ip}") | ||
|
||
if ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("10.0.0.0/8"): | ||
return "A类(私有)" | ||
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("172.16.0.0/12"): | ||
return "B类(私有)" | ||
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("192.168.0.0/16"): | ||
return "C类(私有)" | ||
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("127.0.0.0/8"): | ||
return "Localhost" | ||
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("169.254.0.0/16"): | ||
return "ZeroConf (Apipa/Bonjour)" | ||
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("100.64.0.0/10"): | ||
return "Internal routing (RFC 6598)" | ||
|
||
# 判断A、B、C、D、E类 | ||
if ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("0.0.0.0/8"): | ||
return "A类" | ||
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("128.0.0.0/8"): | ||
return "B类" | ||
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("192.0.0.0/8"): | ||
return "C类" | ||
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("224.0.0.0/4"): | ||
return "D类" | ||
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("240.0.0.0/4"): | ||
return "E类" | ||
|
||
return "无效的CIDR范围" | ||
|
||
# 验证CIDR格式 | ||
def validate_cidr(cidr): | ||
# 检查CIDR是否符合格式,例如:10.0.0.0/8 或 /24 | ||
cidr_pattern = re.compile(r"^(\d{1,3}\.){3}\d{1,3}/\d{1,2}$|^/(\d{1,2})$") | ||
# 改进的正则表达式,确保匹配完整的IP地址(0-255) | ||
cidr_pattern = re.compile(r"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/([0-9]|[1-2][0-9]|3[0-2])$") | ||
if not cidr_pattern.match(cidr): | ||
return "无效的CIDR格式" | ||
|
||
# 如果是完整的CIDR,验证IP和CIDR位数是否合法 | ||
if cidr.startswith('192.168.1.0'): | ||
cidr_bits = int(cidr.split('/')[1]) | ||
if '/' in cidr: | ||
ip, cidr_bits = cidr.split('/') | ||
cidr_bits = int(cidr_bits) | ||
if not (0 <= cidr_bits <= 32): | ||
return "无效的CIDR位数" | ||
|
||
return "valid" | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.