Python数组函数

Python数组函数,python,mysql,arrays,Python,Mysql,Arrays,我正忙着为我的网络嗅探器编写一个阅读器。它将所有类型的数据写入MySQL数据库。我正试图以可读的方式将数据打印到控制台中。我构建了两个函数来打印我的数据 def printHeader(destination, source, protocol): print('Ethernet Frame:') print(('| - Destination: {}, Source: {}, Protocol: {}') .format(destination, source, protocol)

我正忙着为我的网络嗅探器编写一个阅读器。它将所有类型的数据写入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
现在,当我运行查询时,我检索到如下内容:

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

我正在尝试迭代数组,以便将正确的数组索引发送给函数

示例:应将第一个数组[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
DNS=openCON(查询)
z=1
x=2
c=3
a=4
s=5
d=6
g=7
h=8
j=9
k=10
l=11
p=12
尽管如此:
如果p
有人能帮我朝着正确的方向去编码这个漂亮而高效的代码吗?我想也许我不应该查询整个表,而应该只查询一行


提前感谢。

您可以在
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]
]
听起来您已经了解了操作,应该在这里使用。在将数组作为参数传递给函数时,还应使用运算符
*

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

使用切片。此功能可以返回如下列表的子列表:

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]
这使您能够轻松定义应该传递给函数的元素。而不是

printHeader(DNS[(z)], DNS[(x)], DNS[(c)])
你可以写

打印头(DNS[:3])