Titus Brown wrote:
I'd be interested in your comments on the underlying code (attached). I
freely admit to not knowing what I'm doing; I basically just hacked and
slashed until I got what looked like the right results. Corrections
and/or comments on the attached code are appreciated & will be
acknowledged.
I'm always glad to see new dnspython uses :) My comments on selected
bits of your code are below.
def dns_a(host, ipaddress, server=None):
"""
>> dns_a <name> <ipaddress> [<name server>]
Assert that <name> resolves to <ipaddress> (and is an A record).
Optionally use the given name server.
"""
if not is_ip_addr(ipaddress):
raise Exception("<ipaddress> parameter must be an IP address, not a
hostname")
for answer in _query(host, 'A', server):
if _compare_hostnames(ipaddress, str(answer)):
return True
no need to use _compare_hostnames() here, and I think it is clearer to write
if ipaddress == answer.address:
return True
def dns_cname(host, cname, server=None):
"""
>> dns_cname <name> <alias_for> [<name server>]
Assert that <name> is a CNAME alias for <alias_for> Optionally use
<name server>.
"""
if is_ip_addr(cname):
raise Exception("<alias_for> parameter must be a hostname, not an IP
address")
for answer in _query(host, 'CNAME', server):
if _compare_hostnames(cname, str(answer)):
return True
Likewise, with a suitably modified _compare_hostnames() (see below), you
can write this as
if _compare_hostnames(cname, answer.target):
return True
Alternatively, since you know answer.target is going to be a
dns.name.Name, you could just convert 'cname' to a name and then do an
'==' comparison, e.g.:
cname = dns.name.from_text(cname) # or do this isinstance
thing (below) if you want to let the cname
# parameter be a string or a dns.name.Name.
if cname == answer.target:
return True
def dns_mx(host, mailserver, server=None):
"""
>> dns_mx <name> <mailserver> [<name server>]
Assert that <mailserver> is a mailserver for <name>.
"""
for rdata in _query(host, 'MX', server):
(pref, n) = str(rdata).split()
Use rdata.exchange to get the mail server name rather than stringifying
and splitting.
def _compare_hostnames(a, b):
"""
Normalize two hostnames for string comparison. (basically strip off
'.'s ;)
"""
a = a.strip('.')
b = b.strip('.')
return (a == b)
You should let dnspython do the heavy lifting, as comparing domain names
yourself is annoying and error prone. E.g. r'foo.', r'FOO.', and
r'\070\079\079.' are all equal, but your code would return False.
import dns.name
def _compare_hostnames(a, b):
if isinstance(a, (str, unicode)):
a = dns.name.from_text(a)
if isinstance(b, (str, unicode)):
b = dns.name.from_text(b)
return a == b
(note that dns.name.from_text() on a unicode string only works with the
development version of dnspython at the moment)
def is_ip_addr(name):
"""
Check the 'name' to see if it's just an IP address. Probably should use
a regexp to do this... @CTB
"""
name = name.replace('.', '')
for i in range(0, 10):
i = str(i)
name = name.replace(i, '')
if name:
return False
return True
You can let dnspython and the python library do the work here too.
import dns.ipv4
def is_ip_addr(text):
try:
v = dns.ipv4.inet_aton(text)
return True
except socket.error:
return False
_______________________________________________
dnspython-users mailing list
[email protected]
http://woof.play-bow.org/mailman/listinfo/dnspython-users