【发布时间】:2017-01-10 22:18:24
【问题描述】:
我正在尝试将某些 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
它看起来像这样。
IP Address Common Name Valid To
------------ ---------------------- ----------
10.0.255.250 ex.example.com 2017/02/09
IP Address Common Name Valid To
------------ ------------- ----------
10.0.255.251 localhost 2009/07/04
None
IP Address Common Name Valid To
------------ ------------- ----------
10.0.255.252 ex.example2.com 2016/05/2
我一直在尝试使用 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())
【问题讨论】:
-
您将分别对每个
line in text_file进行制表。你有一个for(每个)循环在你的while循环中;外循环只运行一次!尝试将tabulate移出for循环,并完全摆脱while。 -
另外,请注意: 1. 您永远不会尝试做您在标题中描述的事情;和 2. 发现
endline是多余的 -\n是(不可避免地!)行中的最后一个字符。 -
@jonrsharpe End 是我用作输入的文本文件中的最后一个单词。我希望每个循环都存储三列,并在遍历输入文档后,以正确的列顺序将这些值打印到行。
标签: python for-loop while-loop