Reputation: 81
I'm busy writing a reader for my network sniffer. It writes all sort of data into a MySQL database. I'm trying to print the data in a readable way into my console. I've build two function to print my data
def printHeader(destination, source, protocol):
print('Ethernet Frame:')
print(('| - Destination: {}, Source: {}, Protocol: {}')
.format(destination, source, protocol))
def printDNS(version, header_length, ttl, protocolEGP, source, target, source_port, destination_port, length):
print('| - IPv4 Packet:')
print((' | - Version: {}, Header Length: {}, TTL: {},'
).format(version, header_length, ttl))
print((' | - Protocol: {}, Source: {}, Target: {}'
).format(protocolEGP, source, target))
print(' | - UDP Segment:')
print((' | - Source Port: {}, Destination Port: {}, '
'Length: {}').format(source_port, destination_port, length))
def openCON(query):
conn = cymysql.connect(host='*', port=3306, user='root', passwd='*', db='python')
cur = conn.cursor()
cur.execute(query)
data = []
for row in cur:
data.extend(row)
cur.close()
conn.close()
return data
Now when I run a query I retrieve something like this:
[19, '88:9F:FA:D3:0B:5F', '192.168.178.24', 8, 4, 20, 64, 17, '212.54.40.25', 52698, 53, 28485, 18, '88:9F:FA:D3:0B:5F', '192.168.178.24', 8, 4, 20, 64, 17, '212.54.40.25', 52681, 53, 28485, 20, '88:9F:FA:D3:0B:5F', '192.168.178.24', 8, 4, 20, 64, 17, '212.54.40.25', 34310, 53, 28502]
I'm trying to iterate over the array to send the correct array indexes to the functions
Example : first array[1:3] should be sent to printHeader() the second part array[4:11] should be sent to printDNS() the first array is the packet number, not using that one in this example. then I needs to send array [13:15] to printHeader() etc.
I can't seem to figure how to code this in a nice fashion, if tried something like, but as we all know its horrible; also tried looping but with none success so far
DNS = openCON(query)
z = 1
x = 2
c = 3
a = 4
s = 5
d = 6
g = 7
h = 8
j = 9
k = 10
l = 11
p = 12
while True:
if p < len(DNS):
printHeader(DNS[(z)], DNS[(x)], DNS[(c)])
printDNS(DNS[(a)], DNS[(s)], DNS[(d)], DNS[(g)], DNS[(h)], DNS[(j)], DNS[(k)], DNS[(l)], DNS[(p)])
z += 12
x += 12
c += 12
a += 12
s += 12
d += 12
g += 12
h += 12
j += 12
k += 12
l += 12
else:
break
Can somebody help me push in the right direction to code this nice and efficient? I've thought maybe I should not query the entire table but only a row at the time.
Thanks in advance.
Upvotes: 0
Views: 140
Reputation: 76204
Instead of putting mulitple dns data in a single list, you could construct a list of lists in openCON
. Just replace extend
with append
. Then the return value will be more logically laid out:
[
[19, '88:9F:FA:D3:0B:5F', '192.168.178.24', 8, 4, 20, 64, 17, '212.54.40.25', 52698, 53, 28485],
[18, '88:9F:FA:D3:0B:5F', '192.168.178.24', 8, 4, 20, 64, 17, '212.54.40.25', 52681, 53, 28485],
[20, '88:9F:FA:D3:0B:5F', '192.168.178.24', 8, 4, 20, 64, 17, '212.54.40.25', 34310, 53, 28502]
]
Sounds like you already know about slice operations, which you should make use of here. You should also use the splat operator, *
, when you pass the arrays as arguments to your functions.
dnsses = openCON(query)
for dns in dnsses:
printHeader(*dns[0:3])
printDNS(*dns[3:])
Upvotes: 1
Reputation: 927
Use slices. This feature can return sublist of a list like this:
a = [1, 2, 3, 4,5]
a[2:3] # [3]
a[2:5] # [3, 4, 5]
a[-4:-2] # [2, 3]
a[1:3] # [2, 3]
This gices you ability easily define what elements should be passed to a function. Instead of
printHeader(DNS[(z)], DNS[(x)], DNS[(c)])
you can write
printHeader(DNS[:3])
Upvotes: 0