#! /usr/bin/env python""" Extract SIP messages from a file and store them in a file named by its call-id """# import the module needed for regular expressionsimport re# regexp to match sip messagessipmessage = re.compile(r'BYE|INVITE|SIP|ACK|CANCEL|OPTIONS|MESSAGE|PRACK|REFER|INFO|SUBSCRIBE|NOTIFY')# regexp to match Call-IDsipcallid = re.compile(r'Call-ID')# dictionnary that'll hold the call-id named filesdictfiles = {}filetraces = open('traces.txt', 'r') # open the file containing the traces in read modetempline = filetraces.readline() # iterate through the file until the end of the filewhile templine: if sipmessage.match(templine): # there is a sip message on this line callid_line = filetraces.readline() # find its call id while not sipcallid.match(callid_line): callid_line = filetraces.readline() filename = callid_line[9:-1] # take out the "call-id : " at the beginning and the "\n" at the end if not filename in dictfiles: # is this file already created dictfiles[filename] = open(filename, 'a') # if not, open the file in append mode dictfiles[filename].write(templine) # append the sip message templine = filetraces.readline()filetraces.close()for filename in dictfiles: # close all the files that have been opened dictfiles[filename].close()