I'm trying to do a zonetransfer with dnspython
, and then store the A record's into a dictionary that i can pop values from later. See below:
import dns.zone
import dns.query
from dns.exception import DNSException
from dns.rdataclass import *
from dns.rdatatype import *
domain = "mydomain.com"
ns = '1.2.3.4'
try:
zone = dns.zone.from_xfr(dns.query.xfr(ns, domain))
print "Zone origin:", zone.origin
except DNSException, e:
print e.__class__, e
for (name, ttl, rdata) in zone.iterate_rdatas('A'):
record = { name : rdata }
print name, rdata
print record
why is the output when i use print
is different than when i store the same variables name, rdata
in a dictionary, and print that dict? See output below:
www 1.1.1.1
{<DNS name www>: <DNS IN A rdata: 1.1.1.1>}
I guess to clarify my question: How do I make the dict look like :
{ 'www' : '1.1.1.1' }
This is happening because the contents of a dict are represented by calling
repr
on each of its keys/values, whereas printing an object directly will callstr
.Example:
There's not a whole lot you can do about this... You could write your own version of the classes you're trying to print and define
__repr__
so it behaves identically to__str__
. Or you could write a custom dict-printing function that renders the result of__str__
instead of__repr__
. But there's no built-in way to forceprint somedict
to use__str__
instead of__repr__
.