我拥有用于抓取NS
个域记录的简单python代码:
#!/usr/bin/python
import socket
import dns.resolver
domain = 'google.com'
resp = dns.resolver.query(domain, 'NS')
for d in resp:
ns = d.to_text()
nsip = socket.gethostbyname(ns)
print ns, nsip
示例结果如下:
ns2.google.com. 216.239.34.10
ns1.google.com. 216.239.32.10
ns3.google.com. 216.239.36.10
ns4.google.com. 216.239.38.10
ns5.google.com. 216.5.5.5
但是我想从输出中删除打印重复的ip,如下所示:
IP: 216.239.32.10
ns2.google.com., ns1.google.com., ns3.google.com., ns4.google.com.
IP: 216.5.5.5
ns5.google.com.
我该怎么做?
答案 0 :(得分:0)
您可以使用defaultdict
,
In [10]: from collections import defaultdict
In [11]: ns = [['ns2.google.com.', '216.239.32.10'], ['ns1.google.com.', '216.239.32.10'], ['ns3.google.com.', '216.239.36.10'], ['ns4.google.c
...: om.', '216.239.38.10'], ['ns5.google.com.', '216.5.5.5']]
In [12]: d = defaultdict(list)
In [13]: for v,k in ns:
...: d[k].append(v)
In [14]: print d
Out[14]:
defaultdict(list,
{'216.239.32.10': ['ns2.google.com.', 'ns1.google.com.'],
'216.239.36.10': ['ns3.google.com.'],
'216.239.38.10': ['ns4.google.com.'],
'216.5.5.5': ['ns5.google.com.']})
您要打印的输出,
In [16]: for k,v in d.items():
...: print "IP: {}".format(k)
...: print "\t{}".format(', '.join(v))
...:
IP: 216.239.36.10
ns3.google.com.
IP: 216.239.32.10
ns2.google.com., ns1.google.com.
IP: 216.5.5.5
ns5.google.com.
IP: 216.239.38.10
ns4.google.com.
...: