Is there a good regex for multi-line matching of received SIP invitations?

I really need a python regexp that would give me this information:

Data:

Retrieved from 1.1.1.1 18: 41: 51: 330 (123 bytes):

WE INVITE: sip: dsafsdf@fsdafas.com To: sdfasdfasdfas
From: "test"
Via: sdafsdfasdfasd

Sent from 1.1.1.1 18: 42: 51: 330 (123 bytes):

WE INVITE: sip: dsafsdf@fsdafas.com From: "test" To: sdfasdfasdfas Via: sdafsdfasdfasd



Retrieved from 1.1.1.1 18: 50: 51: 330 (123 bytes):

WE INVITE: sip: dsafsdf@fsdafas.com Via: sdafsdfasdfasd From: "test" To: sdfasdfasdfas



, INVITE, "", From: header. .

?:)

.

+3
2

, , ( cr/nl):

sorted(re.findall("Received [^\r\n]+ (\d{2}:\d{2}:\d{2}:\d{3})[^\"]+From: \"([^\r\n]+)\"", data))[-1][1]

RE [^\r\n]. . , . , ...;)

+2

, . , .

import re
import collections

_msg_start_re = re.compile('^(Received|Sent)\s+from\s+(\S.*):\s*$')
_msg_field_re = re.compile('^([A-Za-z](?:(?:\w|-)+)):\s+(\S(?:.*\S)?)\s*$')
def message_parser():
    hdr = None
    fields = collections.defaultdict(list)
    msg = None
    while True:
        if msg is not None:
            line = (yield msg)
            msg = None
            hdr = None
            fields = collections.defaultdict(list)
        else:
            line = (yield None)
        if hdr is None:
            hdr_match = _msg_start_re.match(line)
            hdr = None if hdr_match is None else hdr_match.groups()
        elif len(fields) <= 0:
            field_match = _msg_field_re.match(line)
            if field_match is not None:
                fields[field_match.group(1)].append(field_match.group(2))
        else: # Waiting for the end of the message
            if line.strip() == '':
                msg = (hdr, dict(fields))
            else:
                field_match = _msg_field_re.match(line)
                fields[field_match.group(1)].append(field_match.group(2))

:

parser = msg_parser()
parser.next()
recvd_invites = [msg for msg in (parser.send(line) for line in linelst) \
                   if (msg is not None) and \
                      (msg[0][0] == 'Received') and \
                      ('INVITE' in msg[1])]

, , , , . , - , , .

- , . , , , msg = parser.send(line) , , , ( , msg None).

0

All Articles