I am Listening SMDR call history log from PBX via TCP listening. It working fine continouse running. But reconnecting Windows server after certain period of time PBX giving large amount of data, unable to parser the lines.
parser = re.compile(';')
fieldlist = (
( "start_date", 'date', '%d.%m.%y' ),
( "start_time", 'time', '%H:%M:%S' ),
( "trunk_number", 'str', 2 ),
( "internal_station", 'str', 4 ),
( "ring_duration", 'str',5 ),
( "call_duration", 'time', '%H:%M:%S' ),
( "external_station", 'str', 15 ),
( "call_charge", 'str', 255 ),
( "call_info", 'str', 4 ),
( "ac_ct", 'str', 255 ),
( "msn_charge", 'str', 255 ),
( "sip_station", 'str', 6 ),
( "lcr_route", 'str', 10 ),
( "inout_station", 'str', 15 ),
( "remarks", 'str', 255 ),
);
peerinfo = self.request.getpeername()
log.info(u'Got connection from ' + str(peerinfo[0]) + ' (' + str(peerinfo[1]) + ')')
#Receive data loop
global server_running
dbuffer = ""
while server_running:
data = self.request.recv(1024)
ddata = data.decode('utf-8')
time.sleep(1)
#print (data)
#print (ddata)
if not data:
break
# Append data to LOGFILE
lgf = open(LOGFILE, 'ab')
lgf.write(data)
lgf.close()
s_data = ddata.split(';')
# Process data
line = ddata.strip("\r\n")
vals = parser.split(line)
print (vals)
#print ("internal Station", s_data[3])
#print ("enternal number", s_data[6])
#print ("call info", s_data[8])
#print ("\n")
if len(vals) >= len(fieldlist):
dictv = {}
i = 0
try:
for v in fieldlist:
if v[1] == 'date':
dictv[v[0]] = datetime.strptime(vals[i], v[2]).date()
elif v[1] == 'time':
dictv[v[0]] = datetime.strptime(vals[i], v[2]).time()
elif v[1] == 'int':
dictv[v[0]] = int(vals[i])
elif v[1] == 'str':
if len(vals[i]) > v[2]:
raise ParserError(v[0] + ': String too long')
dictv[v[0]] = str(vals[i])
else:
raise ParserError(v[0] + ': Unknown field type ' + v[1])
i += 1
except Exception as e:
print ("Unable to parse line")
log.error(u"Parse error on line (" + str(v[0]) + str(vals[i]) + "): got exception " + str(e) + " (" + str(line) + ")")
else:
print ("Line parsed correctly")
log.debug(u"Correctly parsed 1 line: " + str(dictv))
#Prepare dictv for query
map(lambda v: MySQLdb.string_literal(v), dictv)
dictv['table'] = MYSQL_DB['table']
Receiving data format as shown below connection successful b'\r\n05.09.19;16:18:53;1;6200;;00:14:32;04222309088;;2;;;0;2;;\r\n05.09.19;16:20:05;69;6258;;00:02:13;;;2;;;7510;1;;\r\n05.09.19;16:21:23;2;6210;00:00;00:00:00;261074;;2;;;0;2;;\r\n05.09.19;16:21:50;70;6258;00:03;00:00:51;7524;;1;;;;;6258;\r\n05.09.19;16:22:15;2;6335;00:21;00:00:00;7639177929;;2;;;0;2;;\r\n05.09.19;16:22:16;69;6320;00:01;00:01:50;7211;;1;;;;;6320;\r\n05.09.19;16:23:31;1;6333;00:03;00:02:19;07708841435;;5;;;;;;\r\n05.09.19;16:23:31;71;6333;;00:02:19;;;9;;;7820;1;;\r\n' ['05.09.19', '16:18:53', '1', '6200', '', '00:14:32', '04222309088', '', '2', '', '', '0', '2', ''] Line parsed correctly b'05.09.19;16:24:34;69;6500;;00:00:07;;;9;;;7815;1;;\r\n05.09.19;16:26:58;1;6335;;00:00:26;7639177929;;2;;;0;2;;\r\n05.09.19;16:27:15;69;6221;00:05;00:00:23;7505;;1;;;;;6217;\r\n05.09.19;16:28:04;70;6335;;00:01:01;;;2;;;7519;1;;\r\n05.09.19;16:28:34;69;6270;;00:00:58;;;2;;;7505;1;;\r\n05.09.19;16:29:04;69;6500;00:03;00:00:00;;;2;;;7815;1;;\r\n05.09.19;16:29:38;69;6500;;00:00:27;;;9;;;7815;1;;\r\n05.09.19;16:29:58;69;6254;00:02;00:00:12;7820;;1;;;;;6254;\r\n05.09.19;16:29:58;70;6333;;00:00:15;;;9;;;7820;1;;\r\n05.09.19;16:29:58;1;6333;00:10;00:00:15;08042994299;;5;;;;;;\r\n05.09.19;16:33:42;69;6259;00:01;00:00:01;7211;;1;;;;;6259;\r\n' ['5.09.19', '16:24:34', '69', '6500', '', '00:00:07', '', '', '9', '', '', '7815', '1', ''] Line parsed correctly
b'05.09.19;16:34:27;69;6262;00:02;00:00:12;7214;;1;;;;;6262;\r\n' ['5.09.19', '16:34:27', '69', '6262', '00:02', '00:00:12', '7214', '', '1', '', '', '', '', '6262'] Line parsed correctly