this is tcpdump originally in pcap, changed into txt. Input:- sip.txt
Ôò¡ Ü ªkã_¹¦ R R hIÿkRT 4V E`D] @9°Ã'ö%1æËÄÞ÷0ûðSIP/2.0 403 Forbidden
Via: SIP/2.0/UDP XXX.XX.XX.X:57079;branch=94tsjam66cmay5bpswyfta0nebw34zhfctjuuge2baevikbk03opf15t6wvovnb82mjih3v;received=IP;rport=57079
From: "IP" <sip:IP@IP>;tag=0c26cd11
To: <sip:XXXXXX@XXX.XX.XX.XXX>;tag=as3a5a21bf
Call-ID: 88c218486f66a6aa214d483d988dfa9c
CSeq: 2 INVITE
Server: Asterisk
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, PRACK, MESSAGE
Supported: replaces, timer
Reason: Q.850;cause=21
Content-Length: 0
My code
import re
from collections import defaultdict
import io
with io.open('file location','rb',encoding='utf-8') as f:
text = f.readlines()
result = []
blocks = text.split('\n\n\n')
# print(blocks)
print(len(blocks))
IP_add_dict_list = defaultdict(list)
IP_add_dict_set = defaultdict(set)
for block in blocks:
if ("CSeq: 1 INVITE" or "CSeq: 1 INVITE") in block:
caller = r";received=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3};"
x = re.search(caller, block)
callerIP = x.group()
callerIP = callerIP[10:-1]
to = r"To: <sip:(\d|[+]|(0-9))*@"
y = re.search(to, block)
toSip = y.group()
toSip = toSip[9:-1]
call = (callerIP, toSip)
result.append(call)
# print(callerIP, toSip)
IP_add_dict_list[callerIP].append(toSip)
IP_add_dict_set[callerIP].add(toSip)
print(IP_add_dict_list)
print("---------------")
print(IP_add_dict_set)
My Output:- error
with io.open('file location', 'rb', encoding='utf-8') as f:
ValueError: binary mode doesn't take an encoding argument