refactor(main.py): 优化 UDP 头部构造代码
- 在 UDP 头部构造中添加注释,解释长度固定为 8 的原因 - 保留原有功能和逻辑不变,仅增加代码可读性
This commit is contained in:
parent
41d46dc492
commit
aa143115d5
5
main.py
5
main.py
@ -126,7 +126,8 @@ def construct_udp(src_port, dest_port):
|
||||
bytes: 构造的UDP报文。
|
||||
"""
|
||||
# UDP头部格式: 源端口号 (2 字节), 目标端口号 (2 字节), 长度 (2 字节), 校验和 (2 字节)
|
||||
udp_header = struct.pack('!HHHH', src_port, dest_port, 8, 0) # 长度固定为8,校验和暂时为0
|
||||
# 长度固定为8,因为UDP头部长度为8字节,校验和暂时为0,稍后由IP层计算并填充
|
||||
udp_header = struct.pack('!HHHH', src_port, dest_port, 8, 0)
|
||||
return udp_header
|
||||
|
||||
# 构造 TCP 报文(SYN)
|
||||
@ -251,6 +252,7 @@ def send_packet():
|
||||
发送报文。
|
||||
"""
|
||||
try:
|
||||
# 获取源IP、目标主机和目标端口
|
||||
src_ip = ip_combobox.get()
|
||||
dest_host = dest_entry.get()
|
||||
dest_port = port_entry.get()
|
||||
@ -264,6 +266,7 @@ def send_packet():
|
||||
if not validate_port(dest_port):
|
||||
raise ValueError("端口号无效,必须是有效的正整数(1-65535)")
|
||||
|
||||
# 解析目标IP地址
|
||||
dest_ip = socket.gethostbyname(dest_host)
|
||||
src_port = 12345
|
||||
dest_port = int(dest_port)
|
||||
|
Loading…
Reference in New Issue
Block a user