MoinQ:

DNS/実装/python/dnslib/lame_check/TLD.pyについて、ここに記述してください。

   1 class TLD:
   2   #  get delegation ns for top domain-list input  
   3   def __init__(self):
   4       self.tld = {}     # tld ns list dictionary
   5       self.mroot = '202.12.27.33'   #m.root-servers.net
   6       for line in open('/home/tmaeno/dnslib/TLDs', 'r'):
   7           domain = line.strip()
   8           nslist = self.delegation(self.mroot,domain)
   9           self.tld[domain]= nslist
  10   #    for x in tld:
  11   #       print (x, tld[x])
  12  
  13   def delegation(self,address,domain):
  14     from dnslib.dns import DNSRecord,DNSHeader,DNSQuestion,DNSError,QTYPE,EDNS0
  15     ns = { }   # nslist   (+ ipaddress list) 
  16 
  17     try:
  18         q0 = DNSQuestion(domain, QTYPE.NS)
  19         q = DNSRecord(q=q0)
  20         # print('question ok', q)
  21         a_pkt = q.send(address, 53, tcp=False)
  22         a = DNSRecord.parse(a_pkt)
  23         if a.header.tc :        # Truncated - retry in TCP mode
  24            a_pkt = q.send(address, 53,tcp=True)
  25            a = DNSRecord.parse(a_pkt)
  26         # NXDOMAIN  rcode
  27         if a.header.rcode != 0 : # ['NOERROR', 'NXDOMAIN', 'SERVFAIL', 'REFUSED']:
  28            print ('ZONEX', domain, a.header.rcode)
  29         else:
  30           for r in a.auth:
  31               if r.rtype == QTYPE.NS:
  32                  ns[str(r.rdata)] = []  # address list
  33           for t in a.ar:
  34               if t.rtype == QTYPE.A :
  35                  tt = str(t.rname) 
  36                  if tt in ns :
  37                      ns[tt].append(str(t.rdata))
  38         return ns
  39     except DNSError as e:
  40                 p.error(e)
  41 
  42   def getaddress(self,d):
  43       top = d.split('.')[-1]   # e.g. com
  44       address = ' '
  45       if top in self.tld:
  46          ns = self.tld[top]   # ns list
  47          for x in ns:
  48            address = ns[x][0]
  49            break
  50       else:  # tld unmatch
  51           pass
  52       return address
  53 #if __name__ == '__main__':
  54 #    print (TLD.getaddress('com'))

MoinQ: DNS/実装/python/dnslib/lame_check/TLD.py (last edited 2021-03-04 15:09:36 by ToshinoriMaeno)