我正忙着为我的网络嗅探器编写一个阅读器。它将所有类型的数据写入 MySQL 数据库。我正在尝试以可读的方式将数据打印到我的控制台中。我已经构建了两个函数来打印我的数据
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
现在,当我运行查询时,我检索到如下内容:
[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]
我正在尝试遍历数组以将正确的数组索引发送到函数
示例:第一个数组[1:3] 应该发送到 printHeader() 第二部分数组[4:11] 应该发送到 printDNS() 第一个数组是包号,在本例中不使用那个。然后我需要将数组 [13:15] 发送到 printHeader() 等。
如果尝试类似的方法,我似乎无法弄清楚如何以一种不错的方式对其进行编码,但众所周知,它很可怕;也尝试过循环,但到目前为止没有成功
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
有人可以帮助我朝着正确的方向前进,以编写这个漂亮而高效的代码吗?我想也许我不应该查询整个表,而只查询一行。
提前致谢。