本文整理汇总了Python中dnslib.DNSRecord.set_header_qa方法的典型用法代码示例。如果您正苦于以下问题:Python DNSRecord.set_header_qa方法的具体用法?Python DNSRecord.set_header_qa怎么用?Python DNSRecord.set_header_qa使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dnslib.DNSRecord
的用法示例。
在下文中一共展示了DNSRecord.set_header_qa方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: serve
# 需要导入模块: from dnslib import DNSRecord [as 别名]
# 或者: from dnslib.DNSRecord import set_header_qa [as 别名]
def serve(self):
try:
udpsocket = socket(AF_INET, SOCK_DGRAM)
udpsocket.bind((self.ip, self.port))
self.socket = udpsocket
logging.getLogger().debug("now serving on %s/%s" % (self.ip, self.port))
while self.stayAlive:
try:
data, addr = self.socket.recvfrom(512)
d = DNSRecord.parse(data)
# print "Question from ",addr
# print d
question = d.get_q()
qname = str(question.qname)
qtype = str(QTYPE[question.qtype]).upper()
try:
ansdict = self.rbldnsd.lookup(qname)
# logging.getLogger().debug("ansdict: %s"%ansdict)
if qtype == "SOA":
if ansdict["SOA"] != None:
response = DNSRecord(DNSHeader(id=d.header.id, gr=1, aa=1, ra=1, qr=1, q=d.get_q()))
soa = ansdict["SOA"]
packet = SOA()
packet.set_mname(soa[0])
packet.set_rname(soa[1])
packet.times = soa[2:]
if "SOATTL" in ansdict:
packet.ttl = ansdict["SOATTL"]
response.rr.append(packet)
self.socket.sendto(response.pack(), addr)
continue
else:
self.send_nxdomain(d, addr)
continue
elif qtype == "NS":
if ansdict["NS"] != None:
# TODO
pass
else:
self.send_nxdomain(d, addr)
continue
elif qtype == "A" or qtype == "TXT":
if "results" not in ansdict:
self.send_nxdomain(d, addr)
logging.getLogger().debug("client=%s q=%s %s -> NXDOMAIN" % (addr[0], qname, qtype))
continue
anslist = ansdict["results"]
anspacklist = []
for answer in anslist:
if answer == None:
continue
if qtype not in answer:
continue
packet = RR(
question.qname, question.qtype, rdata=RDMAP[QTYPE[question.qtype]](answer[qtype])
)
if "TTL" in answer and answer["TTL"] != None:
packet.ttl = answer["TTL"]
anspacklist.append(packet)
if len(anspacklist) > 0:
response = DNSRecord(
DNSHeader(id=d.header.id, bitmap=d.header.bitmap, aa=1, ra=0, qr=1, q=1)
)
response.add_question(question)
response.rr.extend(anspacklist)
response.set_header_qa()
# logging.getLogger().debug(response)
# make sure answer bit is set
# response.header.qr=1
self.socket.sendto(response.pack(), addr)
logging.getLogger().debug("client=%s q=%s %s -> NOERROR" % (addr[0], qname, qtype))
else:
self.send_nxdomain(d, addr)
logging.getLogger().debug("client=%s q=%s %s -> NXDOMAIN" % (addr[0], qname, qtype))
continue
else:
logging.getLogger().warning("unsupported qtype %s" % qtype)
except:
fmt = traceback.format_exc()
logging.getLogger().error(fmt)
self.send_servfail(d, addr)
continue
except Exception:
fmt = traceback.format_exc()
logging.getLogger().error(fmt)
except:
fmt = traceback.format_exc()
logging.getLogger().error("Could not start serversocket on %s/%s: %s" % (self.ip, self.port, fmt))
logging.getLogger().debug("serve() complete")