refactor(main): 优化代码结构并添加注释
- 为关键代码段添加注释,提高代码可读性 - 优化变量命名,使其更具描述性- 调整代码布局,增强可维护性
This commit is contained in:
parent
2f202448f7
commit
8d385e54e2
25
main.py
25
main.py
@ -14,9 +14,9 @@ def get_local_info():
|
|||||||
Returns:
|
Returns:
|
||||||
tuple: 包含主机名、MAC地址和IP地址列表的元组。
|
tuple: 包含主机名、MAC地址和IP地址列表的元组。
|
||||||
"""
|
"""
|
||||||
hostname = socket.gethostname()
|
hostname = socket.gethostname() # 获取主机名
|
||||||
local_ips = socket.gethostbyname_ex(hostname)[-1]
|
local_ips = socket.gethostbyname_ex(hostname)[-1] # 获取本机的IP地址列表
|
||||||
mac = ':'.join(re.findall('..', '%012x' % uuid.getnode()))
|
mac = ':'.join(re.findall('..', '%012x' % uuid.getnode())) # 获取MAC地址
|
||||||
return hostname, mac, local_ips
|
return hostname, mac, local_ips
|
||||||
|
|
||||||
# 验证 IP 地址或域名
|
# 验证 IP 地址或域名
|
||||||
@ -30,8 +30,8 @@ def validate_ip_or_domain(value):
|
|||||||
Returns:
|
Returns:
|
||||||
bool: 如果输入值有效返回True,否则返回False。
|
bool: 如果输入值有效返回True,否则返回False。
|
||||||
"""
|
"""
|
||||||
ip_pattern = re.compile(r'^\d{1,3}(\.\d{1,3}){3}$')
|
ip_pattern = re.compile(r'^\d{1,3}(\.\d{1,3}){3}$') # 匹配IPv4地址的正则
|
||||||
domain_pattern = re.compile(r'^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$')
|
domain_pattern = re.compile(r'^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$') # 匹配域名的正则
|
||||||
return ip_pattern.match(value) or domain_pattern.match(value)
|
return ip_pattern.match(value) or domain_pattern.match(value)
|
||||||
|
|
||||||
# 验证端口号
|
# 验证端口号
|
||||||
@ -64,14 +64,14 @@ def construct_ip_header(src_ip, dest_ip, payload_length, protocol):
|
|||||||
version = 4
|
version = 4
|
||||||
ihl = 5
|
ihl = 5
|
||||||
tos = 0
|
tos = 0
|
||||||
total_length = 20 + payload_length
|
total_length = 20 + payload_length # IP头部20字节 + 数据部分
|
||||||
identification = 54321
|
identification = 54321
|
||||||
flags_offset = 0
|
flags_offset = 0
|
||||||
ttl = 64
|
ttl = 64
|
||||||
header_checksum = 0
|
header_checksum = 0
|
||||||
|
|
||||||
src_ip_bytes = socket.inet_aton(src_ip)
|
src_ip_bytes = socket.inet_aton(src_ip) # 将源IP转换为二进制格式
|
||||||
dest_ip_bytes = socket.inet_aton(dest_ip)
|
dest_ip_bytes = socket.inet_aton(dest_ip) # 将目标IP转换为二进制格式
|
||||||
ip_header = struct.pack('!BBHHHBBH4s4s',
|
ip_header = struct.pack('!BBHHHBBH4s4s',
|
||||||
(version << 4) + ihl, tos, total_length,
|
(version << 4) + ihl, tos, total_length,
|
||||||
identification, flags_offset, ttl, protocol,
|
identification, flags_offset, ttl, protocol,
|
||||||
@ -248,6 +248,7 @@ def send_packet():
|
|||||||
window = tk.Tk()
|
window = tk.Tk()
|
||||||
window.title("网络通信软件")
|
window.title("网络通信软件")
|
||||||
|
|
||||||
|
# 获取本机信息
|
||||||
hostname, mac, local_ips = get_local_info()
|
hostname, mac, local_ips = get_local_info()
|
||||||
|
|
||||||
tk.Label(window, text="本机信息:").grid(row=0, column=0)
|
tk.Label(window, text="本机信息:").grid(row=0, column=0)
|
||||||
@ -256,27 +257,35 @@ info_text.grid(row=0, column=1, columnspan=2)
|
|||||||
info_text.insert(tk.END, f"主机名: {hostname}\nMAC 地址: {mac}\n可用 IP: {', '.join(local_ips)}")
|
info_text.insert(tk.END, f"主机名: {hostname}\nMAC 地址: {mac}\n可用 IP: {', '.join(local_ips)}")
|
||||||
info_text.config(state=tk.DISABLED)
|
info_text.config(state=tk.DISABLED)
|
||||||
|
|
||||||
|
# 配置IP地址选择
|
||||||
tk.Label(window, text="本机 IP 地址:").grid(row=1, column=0)
|
tk.Label(window, text="本机 IP 地址:").grid(row=1, column=0)
|
||||||
ip_combobox = ttk.Combobox(window, values=local_ips)
|
ip_combobox = ttk.Combobox(window, values=local_ips)
|
||||||
ip_combobox.grid(row=1, column=1)
|
ip_combobox.grid(row=1, column=1)
|
||||||
ip_combobox.current(0)
|
ip_combobox.current(0)
|
||||||
|
|
||||||
|
# 配置目标地址输入框
|
||||||
tk.Label(window, text="目标地址:").grid(row=2, column=0)
|
tk.Label(window, text="目标地址:").grid(row=2, column=0)
|
||||||
dest_entry = tk.Entry(window)
|
dest_entry = tk.Entry(window)
|
||||||
dest_entry.grid(row=2, column=1)
|
dest_entry.grid(row=2, column=1)
|
||||||
|
|
||||||
|
# 配置目标端口输入框
|
||||||
tk.Label(window, text="目标端口:").grid(row=3, column=0)
|
tk.Label(window, text="目标端口:").grid(row=3, column=0)
|
||||||
port_entry = tk.Entry(window)
|
port_entry = tk.Entry(window)
|
||||||
port_entry.grid(row=3, column=1)
|
port_entry.grid(row=3, column=1)
|
||||||
|
|
||||||
|
# 配置报文类型选择
|
||||||
tk.Label(window, text="报文类型:").grid(row=4, column=0)
|
tk.Label(window, text="报文类型:").grid(row=4, column=0)
|
||||||
var = tk.StringVar(value="ICMP")
|
var = tk.StringVar(value="ICMP")
|
||||||
tk.Radiobutton(window, text="ICMP", variable=var, value="ICMP").grid(row=4, column=1)
|
tk.Radiobutton(window, text="ICMP", variable=var, value="ICMP").grid(row=4, column=1)
|
||||||
tk.Radiobutton(window, text="UDP", variable=var, value="UDP").grid(row=4, column=2)
|
tk.Radiobutton(window, text="UDP", variable=var, value="UDP").grid(row=4, column=2)
|
||||||
tk.Radiobutton(window, text="TCP", variable=var, value="TCP").grid(row=4, column=3)
|
tk.Radiobutton(window, text="TCP", variable=var, value="TCP").grid(row=4, column=3)
|
||||||
|
|
||||||
|
# 配置结果显示
|
||||||
result = tk.StringVar()
|
result = tk.StringVar()
|
||||||
tk.Label(window, textvariable=result).grid(row=6, column=0, columnspan=3)
|
tk.Label(window, textvariable=result).grid(row=6, column=0, columnspan=3)
|
||||||
|
|
||||||
|
# 发送报文按钮
|
||||||
tk.Button(window, text="发送报文", command=send_packet).grid(row=5, column=1)
|
tk.Button(window, text="发送报文", command=send_packet).grid(row=5, column=1)
|
||||||
|
|
||||||
|
# 启动GUI主循环
|
||||||
window.mainloop()
|
window.mainloop()
|
||||||
|
Loading…
Reference in New Issue
Block a user