以下是对数据集进行源ip、目的ip提取的python3代码(如果是用python2 ,if type[0] == 8 and type[1] == 0:应改为if type==’/x08/x00’).代码原理非常简单,即用read()函数对数据集内容按字节读取,生成元组data。再按照数据报格式把所需要的源ip地址信息、目的ip地址信息、长度信息读取出来,并不断循环直到解析结束为止
pcap文件格式如下图:
import datetime
import struct
import os
import time
def ip_parse():
global timeslot
global start
global user_slots
# 修改文件路径filepath
file = open(‘C:/Users/hp/Documents/Tencent Files/2429185651/FileRecv/t0001’, ‘rb’)
data = file.read() # read函数返回读取的字节
i = 24
j = 40
k = 54
while i < len(data): # python区间左闭右开
seconds = struct.unpack('I', data[i:i + 4])[0] # seconds为由data【i:i+4】解析得到的元组的第一个元素
microseconds = float(struct.unpack('I