本文整理汇总了Python中dns.resolver.Resolver.gethostbyname方法的典型用法代码示例。如果您正苦于以下问题:Python Resolver.gethostbyname方法的具体用法?Python Resolver.gethostbyname怎么用?Python Resolver.gethostbyname使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dns.resolver.Resolver
的用法示例。
在下文中一共展示了Resolver.gethostbyname方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import gethostbyname [as 别名]
def run(self):
""" Run the handler thread """
print "*Ping* We've got a message!"
# Handle DNS request
resolver = Resolver(self.caching, self.ttl)
aliasRecords = []
addressRecords = []
# Read and resolve the questions one-by-one
questions = self.request.questions
for question in questions:
hostname = question.qname
(hostname, aliases, addresses) = resolver.gethostbyname(hostname)
for alias in aliases:
aliasData = dns.resource.RecordData.create(Type.CNAME, alias)
aliasRecord = dns.resource.ResourceRecord(hostname, Type.CNAME, Class.IN, 9001, aliasData) # TODO fix ttl
aliasRecords.append(aliasRecord)
for address in addresses:
addressData = dns.resource.RecordData.create(Type.A, address)
addressRecord = dns.resource.ResourceRecord(hostname, Type.A, Class.IN, 9001, addressData)
addressRecords.append(addressRecord)
# Crafting of the response
respHeader = self.request.header
respHeader.qr = 1
respHeader.qd_count = 0
respHeader.an_count = 1
respMessage = dns.message.Message(respHeader, [], addressRecords + aliasRecords, [], [])
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
respMessageByte = respMessage.to_bytes()
sock.sendto(respMessageByte, self.clientAddr)
print "Ended request: " + hostname
sock.close()