Python 数组函数

标签 python mysql arrays

我正忙着为我的网络嗅探器编写一个阅读器。它将所有类型的数据写入 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() 第二部分 array[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

有人可以帮助我朝着正确的方向前进,以编写出如此漂亮且高效的代码吗?我想也许我不应该查询整个表,而只查询一行。

提前致谢。

最佳答案

您可以在 openCON 中构造一个列表列表,而不是将多个 dns 数据放入单个列表中。只需将 extend 替换为 append 即可。那么返回值的布局就会更加逻辑:

[
    [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]
]

听起来你已经知道 slice操作,您应该在这里使用。您还应该使用 splat当您将数组作为参数传递给函数时,使用运算符 *

dnsses = openCON(query)
for dns in dnsses:
    printHeader(*dns[0:3])
    printDNS(*dns[3:])

关于Python 数组函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19591963/

相关文章:

Python 嵌套函数变量作用域

python - 试图绘制温度

mysql - 这个数据库是3NF吗?

java - 如果用户输入在 Java 中按时间顺序排列,如何迭代数组

c++ - 数组在方括号中增加元素是什么意思?

python - 'x = y or z' 赋值在 Python 中有什么作用?

mysql - 在mysql中查找最大数

mysql - 删除重复行时超慢加载数据 infile

arrays - 按给定键对 Laravel 集合进行分组

python - wxPython:如何使任务栏图标响应左键单击