存储每个循环,并在循环完成时输出到表中

2024-09-30 06:12:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将一些SSL发现扫描的结果输出为一个用于报告目的的表,并且我在以我想要的方式解析这些数据时遇到了一些问题。在

我希望输出如下所示:

IP Address    Common Name             Valid To
------------  ----------------------  ----------
10.0.255.250   ex.example.com  2017/02/09
10.0.255.251   localhost      2009/07/04
10.0.255.252    ex.example2.com     2016/05/24

相反,它看起来像这样。在

^{pr2}$

我一直在尝试使用while循环来解决这个问题,但一直没有任何运气。它似乎不是“while”循环,而是“for each”循环。在

def tabulateText():
    loop = True
    while loop == True:
        with open("testinput.txt", "r") as text_file:
            for line in text_file:
                if "end" in line:
                    loop = False
                elif "IP Address =" in line:
                    start = line.find('IP Address = ')
                    endline = line.find('\n', start)
                    ip = line[start+13:endline]
                    cert = SSLmon(ip)
                    Col1 = ip
                    FCS2 = cert.find('commonName')
                    FCE2 = cert.find('/', FCS2)
                    FCS2b = cert.find('commonName')
                    FCE2b = cert.find('\n', FCS2b)
                    Colopt2 = cert[FCS2+11:FCE2]
                    Colopt2b = cert[FCS2b+11:FCE2b]
                    Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b]
                    FCS3 = cert.find('Not valid after:')
                    FCE3 = cert.find('T', FCS3)
                    Col3 = cert[FCS3+18:FCE3].replace('-', '/')
                    column = Col1[n], Col2[n], Col3[n]
                    print(tabulate([column], headers=['IP Address', 'Common Name', 'Valid To']))
                else:
                    pass

print(tabulateText())
print(tabulateText())
print(tabulateText())

Tags: iniploopcertaddresslinefindstart
2条回答

最后我采取了另一种方法来获取我想要的数据。最后,我打印了每一列,填充由.ljust()控制,如下所示。在

with open("testinput.txt", "r") as text_file:
    for line in text_file:
        if "IP Address =" in line:
            start = line.find('IP Address = ')
            endline = line.find('\n', start)
            ip = line[start+13:endline]
            cert = SSLmon(ip)
            Col1 = ip
            FCS2 = cert.find('commonName')
            FCE2 = cert.find('/', FCS2)
            FCS2b = cert.find('commonName')
            FCE2b = cert.find('\n', FCS2b)
            Colopt2 = cert[FCS2+11:FCE2]
            Colopt2b = cert[FCS2b+11:FCE2b]
            Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b]
            FCS3 = cert.find('Not valid after:')
            FCE3 = cert.find('T', FCS3)
            Col3 = cert[FCS3+18:FCE3].replace('-', '/')
            with open(filename1, 'a') as f:
                line = '%s %s %s %s' % (Col1.ljust(20), Col2.ljust(35), Col3.ljust(15), '\n')
                f.write(line)

print(tablate([column],headers..)命令在每次读取“IP地址”行后执行,这就是为什么您只看到标题和一行数据。在

您可以做的是将结果的每一行(您称之为“column”)追加到一个数组中。这将创建一个包含所有结果的表。在读取文件的所有行之后,执行print(tablate())命令。在

首先,在读取文本文件之前创建一个空数组-我将其称为table:

table = [] 
  with open("testinput.txt", "r") as text_file:

将列数组追加到表数组(表.append[column]),并消除当前使用的print(tablate())命令。在

^{pr2}$

在读取文件并引用新变量“table”之后,移动print(tablate())命令。在

^{3}$

您不需要循环-“for line”命令在文本文件中循环。此外,完成后关闭文件(文本_文件.关闭)在

函数如下所示:

table = []
with open("testinput.txt", "r") as text_file:
    for line in text_file:
        if "IP Address =" in line:
            start = line.find('IP Address = ')
            endline = line.find('\n', start)
            ip = line[start+13:endline]
            cert = SSLmon(ip)
            Col1 = ip
            FCS2 = cert.find('commonName')
            FCE2 = cert.find('/', FCS2)
            FCS2b = cert.find('commonName')
            FCE2b = cert.find('\n', FCS2b)
            Colopt2 = cert[FCS2+11:FCE2]
            Colopt2b = cert[FCS2b+11:FCE2b]
            Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b]
            FCS3 = cert.find('Not valid after:')
            FCE3 = cert.find('T', FCS3)
            Col3 = cert[FCS3+18:FCE3].replace('-', '/')
            column = Col1[n], Col2[n], Col3[n]
            table.append[column]
    text_file.closed
    print(tabulate([table], headers=['IP Address', 'Common Name', 'Valid To']))

如果执行print(table)命令,则表数组本身将如下所示:

[['10.0.255.250','ex.example.com网站','2017/02/09',['10.0.255.251','localhost','2009/07/04',['10.0.255.252','例2.com','2016/05/24']]

相关问题 更多 >

    热门问题