0

print()函数将下面的 dns 查询结果作为字符串输出,但我需要查看此结果的 KEYS。我尝试使用pprint(), getmembers(), vars(), and dir(),但是它们输出了很多不相关的属性,并且不像print()函数那样直接显示结果:

import dns
req = dns.message.make_query('msn.com', dns.rdatatype.ANY)
res = dns.query.udp(req, '8.8.8.8', timeout=3)
print(res)

===========================================================
OUTPUT (Command Prompt)
===========================================================
id 12637
opcode QUERY
rcode NOERROR
flags QR RD RA
;QUESTION
msn.com. IN ANY
;ANSWER
msn.com. 685 IN A 23.101.196.141
msn.com. 21385 IN NS ns3.msft.net.
msn.com. 21385 IN NS ns4.msft.net.
msn.com. 21385 IN NS ns1.msft.net.
msn.com. 21385 IN NS ns2.msft.net.
msn.com. 21385 IN SOA ns1.msft.net. msnhst.microsoft.com. 2015080901 7200 900 2419200 900
msn.com. 685 IN MX 5 mx4.hotmail.com.
msn.com. 685 IN MX 5 mx1.hotmail.com.
msn.com. 685 IN MX 5 mx2.hotmail.com.
msn.com. 685 IN MX 5 mx3.hotmail.com.
msn.com. 685 IN TXT "v=spf1 include:Spf-a.hotmail.com include:Spf-b.hotmail.com include:Spf-c.hotmail.com include:Spf-d.hotmail.com include:_spf-ssg-a.microsoft.com include:Spf.protection.outlook.com ~all"
;AUTHORITY
;ADDITIONAL

如何print()以对象格式(键/值对)获得与函数结果等效的结果?类似于 JSON 对象的外观,这可能吗?

4

0 回答 0