RECEIVER_PORT = 5005 # target port
conn = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
while True:
raw_data, addr = conn.recvfrom(65536)
dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
if eth_proto == 8:
(version, header_length, ttl, proto, src, target, data) = ipv4_Packet(data)
# Sniff UDP Communication between the sender and the receiver
if proto == 17:
src_port, dest_port, length, data = udp_seg(data)
if dest_port == RECEIVER_PORT:
print(TAB_2 + 'Source Port: {}, Destination Port: {}, Length: {}'.format(src_port, dest_port, length))
我的python数据包嗅探器监视两个应用程序之间的通信:发送方和接收方。当数据包被发送到接收器时,它尤其令人感兴趣。另外,接收方仅从发送方接收数据包。每当数据包从发送方发送到接收方时,我的程序都会执行两次“ if dest_port == RECEIVER_PORT”,但我不明白为什么?我希望我的程序一次执行该语句,因为我只从发送方向接收方发送一个数据包。