提取一些SIP标头

时间:2016-05-26 23:23:08

标签: python python-2.7 sip

我正在使用SIP(会话启动协议)URI的正则表达式来提取不同的内部变量。

_syntax = re.compile('^(?P<scheme>[a-zA-Z][a-zA-Z0-9\+\-\.]*):'  # scheme
    + '(?:(?:(?P<user>[a-zA-Z0-9\-\_\.\!\~\*\'\(\)&=\+\$,;\?\/\%]+)' # user
    + '(?::(?P<password>[^:@;\?]+))?)@)?' # password
    + '(?:(?:(?P<host>[^;\?:]*)(?::(?P<port>[\d]+))?))'  # host, port
    + '(?:;(?P<params>[^\?]*))?' # parameters
    + '(?:\?(?P<headers>.*))?$') # headers
m = URI._syntax.match(value)
    if m: 
        self.scheme, self.user, self.password, self.host, self.port, params, headers = m.groups()

我希望提取特定的标题,如标题via,branch,contact,callID或Cseq。 sip消息的一般形式是:

OPTIONS sip:172.16.18.35:5060 SIP/2.0
Content-Length: 0
Via: SIP/2.0/UDP 172.16.18.90:5060
From: "fake" <sip:fake@172.16.18.90>
Supported: replaces, timer
User-Agent: SIPPing
To: <sip:172.16.18.35:5060>
Contact: <sip:fake@172.16.18.90:5060>
CSeq: 1 OPTIONS
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH
Call-ID: fake-id@172.16.18.90
Date: Thu, 25 Apr 2013 003024 +0000
Max-Forwards: 70

1 个答案:

答案 0 :(得分:1)

我建议利用SIP标头格式和RFC822之间的有意识的相似性。

from email.parser import Parser
msg = Parser().parsestr(m.group('headers'))

...此后:

>>> msg.keys()
['Content-Length', 'Via', 'From', 'Supported', 'User-Agent', 'To', 'Contact', 'CSeq', 'Allow', 'Call-ID', 'Date', 'Max-Forwards']
>>> msg['To']
'<sip:172.16.18.35:5060>'
>>> msg['Date']
'Thu, 25 Apr 2013 003024 +0000'

...等。有关详细信息,请参阅the Python standard-library email module的文档。