Reputation: 53
I am using this regular expression for SIP (Session Initiation Protocol) URIs to extract the different internal variables.
_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()
and i want to extract specific header like the header via,branch,contact,callID or Cseq. The general form of a sip message is:
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:[email protected]>
Supported: replaces, timer
User-Agent: SIPPing
To: <sip:172.16.18.35:5060>
Contact: <sip:[email protected]:5060>
CSeq: 1 OPTIONS
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH
Call-ID: [email protected]
Date: Thu, 25 Apr 2013 003024 +0000
Max-Forwards: 70
Upvotes: 1
Views: 1191
Reputation: 295443
I would suggest taking advantage of the intentional similarities between SIP header format and RFC822.
from email.parser import Parser
msg = Parser().parsestr(m.group('headers'))
...thereafter:
>>> 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'
...etc. See the documentation for the Python standard-library email
module for more details.
Upvotes: 2