[英]xlrd: struct.error: unpack requires a string argument of length 512
[英]I am getting an error: “struct.error: unpack requires a string argument of length 14”
我試圖從網絡中捕獲一個數據包,然后將該數據包文件解析為我的python代碼。 該代碼對於捕獲的數據包列表中的前幾個數據包運行得非常好,然后顯示了上述內容
該行中的錯誤:“ l2data = unpack(”!6s6sH“,l2hdr)”
如何更改“拆包”的值以適合我的包裹長度?
我的代碼:
#!usr/bin/python
import pcapy
from struct import*
pcap_file=pcapy.open_offline("single.pcap")
count=1
while count:
print("Packet #:",count)
count=count+1
(header,payload)=pcap_file.next()
l2hdr=payload[:14]
l2data=unpack("!6s6sH",l2hdr)
srcmac="%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:" %(ord(l2hdr[0]),ord(l2hdr[1]),ord(l2hdr[2]),ord(l2hdr[3]),ord(l2hdr[4]),ord(l2hdr[5]))
dstmac="%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:" %(ord(l2hdr[6]),ord(l2hdr[7]),ord(l2hdr[8]),ord(l2hdr[9]),ord(l2hdr[10]),ord(l2hdr[11]))
print("source MAC:",srcmac,"Destination MAC:",dstmac)
ipheader=unpack('!BBHHHBBH4s4s',payload[14:34])
timetolive=ipheader[5]
protocol=ipheader[6]
print("Protocol:",str(protocol)],"Time to live:",str(timetolive))
#!/usr/bin/python
import pcapy
from struct import *
cap = pcapy.open_live("en0", 65536, 1, 0)
while 1:
(header,payload) = cap.next()
l2hdr = payload[:14]
l2data = unpack("!6s6sH", l2hdr)
srcmac = "{0:02x}:{1:02x}:{2:02x}:{3:02x}:{4:02x}:{5:02x}".format(l2hdr[0],l2hdr[1],l2hdr[2],l2hdr[3],l2hdr[4],l2hdr[5])
dstmac = "{0:02x}:{1:02x}:{2:02x}:{3:02x}:{4:02x}:{5:02x}".format(l2hdr[6], l2hdr[7], l2hdr[8], l2hdr[9], l2hdr[10],l2hdr[11])
print("Source MAC: ", srcmac, " Destination MAC: ", dstmac)
ipheader = unpack('!BBHHHBBH4s4s' , payload[14:34])
timetolive = ipheader[5]
protocol = ipheader[6]
print("Protocol ", str(protocol), " Time To Live: ", str(timetolive))
我很確定您的問題與文件有關。 但是,我建議您看一下https://docs.micropython.org/en/latest/pyboard/library/struct.html,以進一步了解解壓縮功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.