-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.py
66 lines (58 loc) · 1.59 KB
/
parse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Parse input file
"""
from collections import namedtuple
Line = namedtuple('Line', [
'timestamp',
'fqdn',
'client_id',
'qtype',
'rcode',
'geoip_lat',
'geoip_long',
'geoip_country'
])
class Parse(object):
def __init__(self, fp):
# string: file path
self.fp = fp
# occurence of FQDNs, {fqdn: number_of_occurences}
self.occurrences = {}
# list of FQDNs
self.fqdn = []
def readline(self):
"""
Read file and return structure of the next line
"""
with file(self.fp) as f:
while 1:
l = f.readline()
if not l:
return None
l = l.split('\t')
return Line(
timestamp=float(l[0]),
fqdn=l[1][:-1],
client_id=l[2],
qtype=int(l[3]),
rcode=int(l[4]),
geoip_lat=float(l[5]),
geoip_long=float(l[6]),
geoip_country=l[7]
)
def compute_fqdn(self):
"""
Parse FQDNs and compute numbers of occurences
"""
with file(self.fp) as f:
while 1:
l = f.readline()
if not l:
return None
l = l.split('\t')
fqdn = l[1][:-1]
if fqdn in self.fqdn:
self.occurrences[fqdn] += 1
else:
self.fqdn.append(l[1][:-1])
self.occurrences[fqdn] = 1