1

嘗試使用python中的raw套接字創建數據包嗅探器,並且想要使用struct.unpack方法解析完整的TCP標頭,但是某些字段像HLEN(4位)和offset,URG,ACK,PST ,TCP報頭中的RST,SYN,FIN位不是字節。所以我的問題是如何從標題解析這些值!使用原始套接字的python數據包嗅探器

回答

0

你可以使用:

下面是一個例子:

from ctypes import c_int32, c_uint32, Structure, Union 

class _bits(Structure): 
    _fields_ = [ 
     ("odd", c_uint32, 1), 
     ("half", c_uint32, 31), 
    ] 

class Int(Union): 
    _fields_ = [ 
     ("bits", _bits), 
     ("number", c_uint32), 
    ] 


a = Int(number=12345) 
a.bits.odd, a.bits.half 

結果:

>>> a = Int(number=12345) 
>>> a.bits.odd, a.bits.half 
(1L, 6172L)