[英]Reading full packet using python raw socket
我正在嘗試使用轉發表為UDP數據包創建原始套接字,以僅進行IP轉發。 我不是原始套接字編程專家,我關心的是如何確保緩沖區中有完整的UDP數據包。
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
s.bind(('0.0.0.0', 1337))
while True:
packet = s.recvfrom(65535) #how to know that I read the complete packet not 50% or 1 and half packet
forwared_packet_function(packet) #here I will parse the packet change ip header and update the IP table and forward the packet
是否可以告訴我如何通過recvfrom()
讀取一個完整的數據包?
我也不是原始套接字編程的專家,但是我對網絡有一定的了解。
第一個問題:
怎么知道我讀的不是完整的數據包而不是50%或1個半數據包?
答:不能。 它沒有像TCP這樣的“序列號”或“確認號”。
第二個問題:
是否可以告訴我如何通過recvfrom()讀取一個完整的數據包?
答:讓我們嘗試
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
s.bind(('0.0.0.0', 1337))
while True:
packet = s.recvfrom(65535).decode() #decode packet
forwared_packet_function(packet)
print(packet) #print packet to read
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.