I’ve made this code but it is not acting how I was expecting:
JavaScript
x
18
18
1
packets = rdpcap(file)
2
for m in packtes:
3
if (raw(m)[14:].decode("ASCII")).find(search)!= -1:
4
riga = (raw(m)[14:].decode("ASCII"))
5
index_1 = riga[riga.index(":"):]
6
index_2 = index_1[1:index_1.index(",")]
7
index_3 = index_2.replace(" ", "")
8
if search == "True":
9
var_1 = 1
10
else:
11
var_2 = 0
12
break
13
else:
14
if test == "nothing":
15
var_1 = 0
16
else:
17
var_2 = 1
18
Basically I read a .pcap file which can be full of information or empty and I’m looking for a word inside each raw. If it is found I start some operation on this string in order to reach a desidered position inside the string.
I noticed that when the .pcap file is empty the code doesn’t enter into the “else” condition. I think I’m making a mistake with the find function in python but I don’t see it.
Advertisement
Answer
JavaScript
1
22
22
1
packets = rdpcap(file)
2
#gets executed when there's content in the file
3
if(packets):
4
for m in packtes:
5
if (raw(m)[14:].decode("ASCII")).find(search)!= -1:
6
riga = (raw(m)[14:].decode("ASCII"))
7
index_1 = riga[riga.index(":"):]
8
index_2 = index_1[1:index_1.index(",")]
9
index_3 = index_2.replace(" ", "")
10
if search == "True":
11
var_1 = 1
12
else:
13
var_2 = 0
14
break
15
else:
16
if test == "nothing":
17
var_1 = 0
18
else:
19
var_2 = 1
20
else:
21
print("Empty file")
22
it will execute the outermost if statement when there’s content in the file otherwise it will execute the else statement.