Python脚本,用于计算两个连续数据包之间的时间间隔

2024-10-02 00:35:39 发布

您现在位置:Python中文网/ 问答频道 /正文

如何计算两个连续数据包之间的时间?这是目前我们要计算的两个数据包之间的时间间隔,有什么改进的建议吗。 这就是我目前的情况:

类FlowDict(对象):

def __init__(self):
    self.TCPactiveFlow = dict()
    self.TCPcompleteFlow = list()
    self.UDPactiveFlow = dict()
    self.UDPcompleteFlow = list()

    self.currenttime = -1
    self.lastupdate = -1
    self.threshold = 300
    self.FLowThreshold = 600


def captureOnePkt(self, srcIP, srcPort, desIP, desPort, protocol, timestamp):

    if "TCP" in line: 
        print "Found a TCP packet."
        #add all the calculations
        if self.currenttime == -1:
            self.currenttime = timestamp

        if self.lastupdate == -1:
            self.lastupdate = timestamp

        TCPkey1 = hash(str(srcIP) + str(srcPort) + str(desIP) + str(desPort))
        TCPkey2 = hash(str(desIP) + str(desPort) + str(srcIP) + str(srcPort))

        if 'TCPkey1' in self.TCPactiveFlow:
            self.TCPactiveFlow[TCPkey1].updateSending(timestamp)
            print("Updated the Sending Time for the Flow.")
        elif 'TCPkey2' in self.TCPactiveFlow:
            self.TCPactiveFlow[TCPkey2].updateReceiving(timestamp)
            print("Updated the Receiving Time for the Flow.")
        else:
            f = Flow(srcIP, desIP, srcPort, desPort, protocol, timestamp)
            self.TCPactiveFlow[TCPkey1] = f

        if(self.currenttime - self.lastupdate > self.threshold):
            self.lastupdate= self.currenttime

            for key in self.TCPactiveFlow:

                if(self.currenttime - self.TCPactiveFlow[TCPkey1].endTime > self.FLowThreshold):
                    self.TCPcompleteFlow.append(self.TCPactiveFlow[TCPkey1])
                    del self.TCPactiveFlow

    if "UDP" in line:
        print "Found a UDP packet."
        #add all the calculations
        if self.currenttime == -1:
            self.currenttime = timestamp

        if self.lastupdate == -1:
            self.lastupdate = timestamp

        UDPkey1 = hash(str(srcIP) + str(srcPort) + str(desIP) + str(desPort))
        UDPkey2 = hash(str(desIP) + str(desPort) + str(srcIP) + str(srcPort))

        if 'UDPkey1' in self.UDPactiveFlow:
            self.UDPactiveFlow[UDPkey1].updateSending(timestamp)
            print("Updated the Sending Time for the Flow.")
        elif 'UDPkey2' in self.UDPactiveFlow:
            self.UDPactiveFlow[UDPkey2].updateReceiving(timestamp)
            print("Updated the Receiving Time for the Flow.")
        else:
            f = Flow(srcIP, desIP, srcPort, desPort, protocol, timestamp)
            self.UDPactiveFlow[UDPkey1] = f

        if(self.currenttime - self.lastupdate > self.threshold):
            self.lastupdate= self.currenttime

            for key in self.UDPactiveFlow:

                if(self.currenttime - self.UDPactiveFlow[UDPkey1].endTime > self.FLowThreshold):
                    self.UDPcompleteFlow.append(self.UDPactiveFlow[UDPkey1])
                    del self.UDPactiveFlow

def TCPwrap(self):    
    for key in self.TCPactiveFlow:
        self.TCPcompleteFlow.append(self.TCPactiveFlow[key])
        del self.TCPactiveFlow

def UDPwrap(self):    
    for key in self.UDPactiveFlow:
        self.UDPcompleteFlow.append(self.UDPactiveFlow[key])
        del self.UDPactiveFlow

def analyze(self):
    #Times between 2 Packets
    if self.currenttime:
        pass
        lastupdate=self.currenttime[-1]
    else:
        lastupdate=False
        now=self.currenttime()
        self.currenttime.append(now)
        if lastupdate:
            return self.currenttime-lastupdate
        else:
            return -1

Tags: theinselfforiftimestampstrcurrenttime

热门问题