DNS/実装/python/dnslib/lame_check/TLD.pyについて、ここに記述してください。 {{{#!python class TLD: # get delegation ns for top domain-list input def __init__(self): self.tld = {} # tld ns list dictionary self.mroot = '202.12.27.33' #m.root-servers.net for line in open('/home/tmaeno/dnslib/TLDs', 'r'): domain = line.strip() nslist = self.delegation(self.mroot,domain) self.tld[domain]= nslist # for x in tld: # print (x, tld[x]) def delegation(self,address,domain): from dnslib.dns import DNSRecord,DNSHeader,DNSQuestion,DNSError,QTYPE,EDNS0 ns = { } # nslist (+ ipaddress list) try: q0 = DNSQuestion(domain, QTYPE.NS) q = DNSRecord(q=q0) # print('question ok', q) a_pkt = q.send(address, 53, tcp=False) a = DNSRecord.parse(a_pkt) if a.header.tc : # Truncated - retry in TCP mode a_pkt = q.send(address, 53,tcp=True) a = DNSRecord.parse(a_pkt) # NXDOMAIN rcode if a.header.rcode != 0 : # ['NOERROR', 'NXDOMAIN', 'SERVFAIL', 'REFUSED']: print ('ZONEX', domain, a.header.rcode) else: for r in a.auth: if r.rtype == QTYPE.NS: ns[str(r.rdata)] = [] # address list for t in a.ar: if t.rtype == QTYPE.A : tt = str(t.rname) if tt in ns : ns[tt].append(str(t.rdata)) return ns except DNSError as e: p.error(e) def getaddress(self,d): top = d.split('.')[-1] # e.g. com address = ' ' if top in self.tld: ns = self.tld[top] # ns list for x in ns: address = ns[x][0] break else: # tld unmatch pass return address #if __name__ == '__main__': # print (TLD.getaddress('com')) }}}