本文整理汇总了Python中dns.resolver.Resolver.timeout方法的典型用法代码示例。如果您正苦于以下问题:Python Resolver.timeout方法的具体用法?Python Resolver.timeout怎么用?Python Resolver.timeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dns.resolver.Resolver
的用法示例。
在下文中一共展示了Resolver.timeout方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: reverse_dns_lookup
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import timeout [as 别名]
def reverse_dns_lookup(input, timeout=3, server=''):
"""Perform a simple reverse DNS lookup, return results in a dictionary"""
assert _REVERSE_DNS_REGEX.search(
input), "Invalid address format: '{0}'".format(input)
resolver = Resolver()
resolver.timeout = float(timeout)
resolver.lifetime = float(timeout)
if server:
resolver.nameservers = [server]
try:
tmp = input.strip().split('.')
tmp.reverse()
inaddr = '.'.join(tmp) + ".in-addr.arpa"
records = resolver.query(inaddr, 'PTR')
return {
'name': records[0].to_text(),
'lookup': inaddr,
'error': '',
'addr': input,
}
except DNSException as e:
return {
'addrs': [],
'lookup': inaddr,
'error': repr(e),
'name': input,
}
示例2: dns6_lookup
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import timeout [as 别名]
def dns6_lookup(input, timeout=3, server=''):
"""Perform a simple DNS lookup, return results in a dictionary"""
resolver = Resolver()
resolver.timeout = float(timeout)
resolver.lifetime = float(timeout)
if server:
resolver.nameservers = [server]
try:
records = resolver.query(input, 'AAAA')
return {'addrs': [ii.address for ii in records],
'error': '',
'name': input,
}
except DNSException as e:
return {'addrs': [],
'error': repr(e),
'name': input,
}
示例3: check_dns
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import timeout [as 别名]
def check_dns(check, data):
check.addOutput("ScoreEngine: {} Check\n".format(check.getServiceName()))
check.addOutput("EXPECTED: Sucessful and correct query against the DNS server")
check.addOutput("OUTPUT:\n")
# Setup the resolver
resolv = Resolver()
resolv.nameservers = [data["HOST"]]
resolv.timeout = dns_config["timeout"]
resolv.lifetime = dns_config["lifetime"]
check.addOutput("Starting check...")
try:
# Query resolver
check.addOutput("Querying {HOST} for '{LOOKUP}'...".format(**data))
lookup = resolv.query(data["LOOKUP"], data["TYPE"])
found = False
for ans in lookup:
if str(ans) == data["EXPECTED"]:
found = True
else:
check.addOutput("NOTICE: DNS Server returned {}".format(ans))
if not found:
check.addOutput("ERROR: DNS Server did not respond with the correct IP")
return
# We're good!
check.setPassed()
check.addOutput("Check successful!")
except Exception as e:
check.addOutput("ERROR: {}: {}".format(type(e).__name__, e))
return
示例4: dns_query
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import timeout [as 别名]
def dns_query(input="", query_type="", server="", timeout=2.0):
"""A unified IPv4 & IPv6 DNS lookup interface; this is essentially just a wrapper around dnspython's API. When you query a PTR record, you can use an IPv4 or IPv6 address (which will automatically be converted into an in-addr.arpa name. This wrapper only supports a subset of DNS records: 'A', 'AAAA', 'CNAME', 'MX', 'NS', 'PTR', and 'TXT'
Kwargs:
- input (str): A string containing the DNS record to lookup
- query_type (str): A string containing the DNS record type (SOA not supported)
- server (str): A string containing the fqdn or IP address of the dns server
- timeout (float): DNS lookup timeout duration (default: 2.0 seconds)
Returns:
A set() of :class:`~ccp_util.DNSResponse` instances
>>> from ciscoconfparse.ccp_util import dns_query
>>> dns_query('www.pennington.net', "A", "4.2.2.2")
set([<DNSResponse "A" result_str="65.19.187.2">])
>>> answer = dns_query('www.pennington.net', 'A', '4.2.2.2')
>>> str(answer.pop())
'65.19.187.2'
>>>
"""
valid_records = set(['A', 'AAAA', 'AXFR', 'CNAME', 'MX', 'NS', 'PTR',
'TXT'])
query_type = query_type.upper()
assert query_type in valid_records
assert server!=""
assert float(timeout)>0
assert input != ""
intput = input.strip()
retval = set([])
resolver = Resolver()
resolver.server = [socket.gethostbyname(server)]
resolver.timeout = float(timeout)
resolver.lifetime = float(timeout)
start = time.time()
if (query_type=="A") or (query_type=="AAAA"):
try:
answer = resolver.query(input, query_type)
duration = time.time() - start
for result in answer:
response = DNSResponse(query_type=query_type,
duration=duration,
input=input, result_str = str(result.address))
retval.add(response)
except DNSException as e:
duration = time.time() - start
response = DNSResponse(input=input,
duration=duration, query_type=query_type)
response.has_error = True
response.error_str = e
retval.add(response)
elif query_type=="AXFR":
"""This is a hack: return text of zone transfer, instead of axfr objs"""
_zone = zone.from_xfr(query.xfr(server, input, lifetime=timeout))
return [_zone[node].to_text(node) for node in _zone.nodes.keys()]
elif query_type=="CNAME":
try:
answer = resolver.query(input, query_type)
duration = time.time() - start
for result in answer:
response = DNSResponse(query_type=query_type,
duration=duration,
input=input, result_str = str(result.target))
retval.add(response)
except DNSException as e:
duration = time.time() - start
response = DNSResponse(input=input, duration=duration,
query_type=query_type)
response.has_error = True
response.error_str = e
retval.add(response)
elif query_type=="MX":
try:
answer = resolver.query(input, query_type)
duration = time.time() - start
for result in answer:
response = DNSResponse(query_type=query_type,
input=input, result_str = str(result.target))
response.preference = int(result.preference)
retval.add(response)
except DNSException as e:
duration = time.time() - start
response = DNSResponse(input=input, duration=duration,
query_type=query_type)
response.has_error = True
response.error_str = e
retval.add(response)
elif query_type=="NS":
try:
answer = resolver.query(input, query_type)
duration = time.time() - start
for result in answer:
response = DNSResponse(query_type=query_type,
duration=duration,
input=input, result_str = str(result.target))
retval.add(response)
except DNSException as e:
duration = time.time() - start
response = DNSResponse(input=input,
duration=duration, query_type=query_type)
#.........这里部分代码省略.........