Sunny
Sunny

Reputation: 2332

How to print object in key/value format?

The print() function outputs the result of a dns query below as a string, but I need to see the KEYS of this result. I tried using pprint(), getmembers(), vars(), and dir(), but they output a lot of irrelevant attributes and don't directly show the result like the print() function does:

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

How can I get an equivalent to the print() function result in object format (key/value pairs)? Something analogous to what a JSON object would look like, is this possible?

Upvotes: 0

Views: 232

Answers (0)

Related Questions