在下面的代码中我从未见过类似
的语法state=nmScan[tgtHost]['tcp'][int(tgtPort)]['state']
这之前。看起来像多个列表。任何人都可以解释一下语法用例。我之前从未在python中看到过如上所述的语法。
import nmap
import optparse
def nmapScan(tgtHost,tgtPort):
nmScan = nmap.PortScanner()
nmScan.scan(tgtHost,
tgtPort)
state=nmScan[tgtHost]['tcp'][int(tgtPort)]['state']
print "[*] " + tgtHost + " tcp/"+tgtPort +" "+state
def main():
parser = optparse.OptionParser('-H <10.10.10.104> -p <20-25>')
parser.add_option('-H',
dest='tgtHost',
type='string',
help='specify target host')
parser.add_option('-p',
dest='tgtPort',
type='string',
help='specify target port[s] separated by comma')
(options, args) = parser.parse_args()
tgtHost = options.tgtHost
tgtPorts = str(options.tgtPort).split(',')
if (tgtHost == None) | (tgtPorts[0] == None):
print parser.usage
exit(0)
for tgtPort in tgtPorts:
nmapScan(tgtHost, tgtPort)
if __name__ == '__main__':
main()
答案 0 :(得分:1)
它只是一系列索引操作。您可以使用临时变量扩展它:
t1 = nmScan[tgtHost]
t2 = t1['tcp']
t3 = t2[int(tgtPort)]
state = t3['state']
由于t1
和nmScan[tgtHost]
引用了同一个对象,因此t2 = t1['tcp']
和t2 = nmScan[tgtHost]
之间没有区别。相同
推理适用于更长的链接。