#! /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 expressions import re # regexp to match sip messages sipmessage = re.compile(r'BYE|INVITE|SIP|ACK|CANCEL|OPTIONS|MESSAGE|PRACK|REFER|INFO|SUBSCRIBE|NOTIFY') # regexp to match Call-ID sipcallid = re.compile(r'Call-ID') # dictionnary that'll hold the call-id named files dictfiles = {} filetraces = open('traces.txt', 'r') # open the file containing the traces in read mode templine = filetraces.readline() # iterate through the file until the end of the file while 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()