本文整理汇总了Python中dns.resolver.NoNameservers方法的典型用法代码示例。如果您正苦于以下问题:Python resolver.NoNameservers方法的具体用法?Python resolver.NoNameservers怎么用?Python resolver.NoNameservers使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dns.resolver
的用法示例。
在下文中一共展示了resolver.NoNameservers方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_rbl
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def test_rbl():
""" handles failure ok, and handles lookups ok with dns module.
"""
import mock
from dns.resolver import NoAnswer, NXDOMAIN, NoNameservers
from pygameweb.user.rbl import rbl
assert rbl('127.0.0.1') is False
assert rbl('') is False
with mock.patch('dns.resolver.query') as query:
query.side_effect = NoAnswer()
assert rbl('192.168.0.1') is False
query.side_effect = NXDOMAIN()
assert rbl('192.168.0.1') is False
query.side_effect = NoNameservers()
assert rbl('192.168.0.1') is False
with mock.patch('dns.resolver.query') as query:
query.side_effect = '127.0.0.2'
assert rbl('192.168.0.1') is True
assert query.called
示例2: check_for_wildcards
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def check_for_wildcards(args, server, name, rectype, tries=4):
"""
Verify that the DNS server doesn't return wildcard results for domains
which don't exist, it should correctly return NXDOMAIN.
"""
resolver = Resolver()
resolver.timeout = args.timeout
resolver.lifetime = args.timeout
resolver.nameservers = [server]
nx_names = [base64.b32encode(
os.urandom(
random.randint(8, 10))
).strip('=').lower() + name
for _ in range(0, tries)]
correct_result_count = 0
for check_nx_name in nx_names:
try:
result = resolver.query(check_nx_name, rectype)
return False # Any valid response = immediate fail!
except (NXDOMAIN, NoNameservers):
correct_result_count += 1
except DNSException:
continue
return correct_result_count > (tries / 2.0)
示例3: resolve_ns
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def resolve_ns(data, t='A', timeout=HUNTER_RESOLVER_TIMEOUT):
resolver = dns.resolver.Resolver()
resolver.timeout = timeout
resolver.lifetime = timeout
resolver.search = []
try:
answers = resolver.query(data, t)
resp = []
for rdata in answers:
resp.append(rdata)
except (NoAnswer, NXDOMAIN, EmptyLabel, NoNameservers, Timeout) as e:
if str(e).startswith('The DNS operation timed out after'):
logger.info('{} - {} -- this may be normal'.format(data, e))
return []
if not str(e).startswith('The DNS response does not contain an answer to the question'):
if not str(e).startswith('None of DNS query names exist'):
logger.info('{} - {}'.format(data, e))
return []
return resp
示例4: get_mx_domains
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def get_mx_domains(domain, dns_resolver=_dns_resolver):
""" Retrieve and return the MX records for a domain. """
mx_records = []
try:
mx_records = dns_resolver().query(domain, 'MX')
except NoNameservers:
log.error('NoMXservers', domain=domain)
except NXDOMAIN:
log.error('No such domain', domain=domain)
except Timeout:
log.error('Time out during resolution', domain=domain)
raise
except NoAnswer:
log.error('No answer from provider', domain=domain)
mx_records = _fallback_get_mx_domains(domain)
return [str(rdata.exchange).lower() for rdata in mx_records]
示例5: query_dns
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def query_dns(cls, domains, records):
"""
Query DNS records for host.
:param domains: Iterable of domains to get DNS Records for
:param records: Iterable of DNS records to get from domain.
"""
results = {k: set() for k in records}
for record in records:
for domain in domains:
try:
answers = cls.resolver.query(domain, record)
for answer in answers:
# Add value to record type
results.get(record).add(answer)
except (resolver.NoAnswer, resolver.NXDOMAIN, resolver.NoNameservers):
# Type of record doesn't fit domain or no answer from ns
continue
return {k: v for k, v in results.items() if v}
示例6: public_suffix
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def public_suffix(self):
try:
public_suffix = psl.get_public_suffix(self.name)
is_public_suffix = psl.is_public_suffix(self.name)
except (Timeout, NoNameservers):
public_suffix = self.name.rpartition('.')[2]
is_public_suffix = ('.' not in self.name) # TLDs are public suffixes
except psl_dns.exceptions.UnsupportedRule as e:
# It would probably be fine to treat this as a non-public suffix (with the TLD acting as the
# public suffix and setting both public_suffix and is_public_suffix accordingly).
# However, in order to allow to investigate the situation, it's better not catch
# this exception. For web requests, our error handler turns it into a 503 error
# and makes sure admins are notified.
raise e
if is_public_suffix:
return public_suffix
# Take into account that any of the parent domains could be a local public suffix. To that
# end, identify the longest local public suffix that is actually a suffix of domain_name.
for local_public_suffix in settings.LOCAL_PUBLIC_SUFFIXES:
has_local_public_suffix_parent = ('.' + self.name).endswith('.' + local_public_suffix)
if has_local_public_suffix_parent and len(local_public_suffix) > len(public_suffix):
public_suffix = local_public_suffix
return public_suffix
示例7: get
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def get(self, rdtype, domain):
t1 = time.time()
rdtype = rdtype.upper()
current_app.logger.info(
'Request from %s - %s', request.remote_addr, rdtype)
self.valid_args(rdtype, domain)
# Iterate through nameservers so that we can tell which one gets used.
nameservers = current_app.config['RESOLVERS']
for nameserver in nameservers:
dns_resolver.nameservers = [nameserver]
try:
answer = dns_resolver.query(
domain, rdtype, raise_on_no_answer=False)
# Successful query
break
except (NoNameservers, NXDOMAIN):
# TODO: this should still follow the RFC
return {'message': "No nameservers found for provided domain"}, 404
except Timeout as e:
# Communication fail or timeout - try next nameserver
if nameserver is nameservers[-1]:
current_app.logger.info(e)
return {'message': 'All nameservers timed out.'}, 503
continue
except Exception as e:
current_app.logger.error(e)
return {'message': 'An unexpected error occured.'}, 500
t2 = time.time()
duration = t2 - t1
return parse_query(answer, nameserver, duration)
示例8: do_query_a
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def do_query_a(domain, resolver):
try:
answer = resolver.query(domain, 'A')
# If resolve random subdomain raise timeout error, try again
except Timeout as e:
logger.log('ALERT', f'DNS resolve timeout, retrying')
logger.log('DEBUG', e.args)
raise tenacity.TryAgain
# If resolve random subdomain raise NXDOMAIN, YXDOMAIN, NoAnswer, NoNameservers error
# It means that there is no A record of random subdomain and not use wildcard dns record
except (NXDOMAIN, YXDOMAIN, NoAnswer, NoNameservers) as e:
logger.log('DEBUG', e.args)
logger.log('INFOR', f'{domain} seems not use wildcard dns record')
return False
except Exception as e:
logger.log('ALERT', f'Detect {domain} wildcard dns record error')
logger.log('FATAL', e.args)
exit(1)
else:
if answer.rrset is None:
logger.log('ALERT', f'DNS resolve dont have result, retrying')
raise tenacity.TryAgain
ttl = answer.ttl
name = answer.name
ips = {item.address for item in answer}
logger.log('ALERT', f'{domain} use wildcard dns record')
logger.log('ALERT', f'{domain} resolve to: {name} '
f'IP: {ips} TTL: {ttl}')
return True
示例9: get_wildcard_record
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def get_wildcard_record(domain, resolver):
logger.log('INFOR', f'Query {domain} \'s wildcard dns record in authoritative name server')
try:
answer = resolver.query(domain, 'A')
# 如果查询随机域名A记录时抛出Timeout异常则重新查询
except Timeout as e:
logger.log('ALERT', f'Query timeout, retrying')
logger.log('DEBUG', e.args)
raise tenacity.TryAgain
except (NXDOMAIN, YXDOMAIN, NoAnswer, NoNameservers) as e:
logger.log('DEBUG', e.args)
logger.log('INFOR', f'{domain} dont have A record on authoritative name server')
return None, None
except Exception as e:
logger.log('ERROR', e.args)
logger.log('ERROR', f'Query {domain} wildcard dns record in authoritative name server error')
exit(1)
else:
if answer.rrset is None:
logger.log('DEBUG', f'No record of query result')
return None, None
name = answer.name
ip = {item.address for item in answer}
ttl = answer.ttl
logger.log('INFOR', f'{domain} results on authoritative name server: {name} '
f'IP: {ip} TTL: {ttl}')
return ip, ttl
示例10: test_record_NoNameservers
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def test_record_NoNameservers(self):
"""
Tests the method which let us get the A record for the case that
we get a NoNameservers exception.
"""
self.dns_lookup.resolver.query = Mock(side_effect=NoNameservers())
expected = None
actual = self.dns_lookup.a_record(self.subject)
self.assertEqual(expected, actual)
示例11: consumer
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def consumer(self):
while True:
try:
hostname, rtype = self.queue.get(True, 5)
except Empty:
logging.debug("Empty! Bailing")
return
try:
logging.debug("Starting work on {}".format(hostname))
results = self.resolver.query(hostname, rtype)
if results:
if hostname not in self.results:
self.results[hostname] = {}
text_results = []
for r in results:
if isinstance(r, NS_class):
text_results.append(r.target.to_text())
elif isinstance(r, A_class):
text_results.append(r.to_text())
else:
logging.error(
"Unknown record type: {}".format(type(r)))
hostname = Hostname(value=hostname)
ResolveHostnames.each(hostname, rtype, text_results)
except NoAnswer:
continue
except NXDOMAIN:
continue
except Timeout:
logging.debug("Request timed out for {}".format(hostname))
continue
except NoNameservers:
continue
except Exception as e:
import traceback
logging.error(
"Unknown error occurred while working on {} ({})".format(
hostname, rtype))
logging.error("\nERROR: {}".format(hostname, rtype, e))
logging.error(traceback.print_exc())
continue
示例12: verify
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def verify(self, email):
"""
method that performs the verification on the passed
email address.
"""
lookup = {
'address': None,
'valid_format': False,
'deliverable': False,
'full_inbox': False,
'host_exists': False,
'catch_all': False,
}
try:
lookup['address'] = self._parse_address(email)
lookup['valid_format'] = True
except EmailFormatError:
lookup['address'] = f"{email}"
return lookup
# look for mx record and create a list of mail exchanges
try:
mx_record = resolver.query(lookup['address'].domain, 'MX')
mail_exchangers = [exchange.to_text().split() for exchange in mx_record]
lookup['host_exists'] = True
except (resolver.NoAnswer, resolver.NXDOMAIN, resolver.NoNameservers):
lookup['host_exists'] = False
return lookup
for exchange in mail_exchangers:
try:
host_exists, deliverable, catch_all = self._can_deliver(exchange, lookup['address'])
if deliverable:
lookup['host_exists'] = host_exists
lookup['deliverable'] = deliverable
lookup['catch_all'] = catch_all
break
except SMTPRecepientException as err:
# Error handlers return a dict that is then merged with 'lookup'
kwargs = handle_error.get(err.code, handle_unrecognised)(err.response)
# This expression merges the lookup dict with kwargs
lookup = {**lookup, **kwargs}
except smtplib.SMTPServerDisconnected as err:
lookup['message'] = "Internal Error"
except smtplib.SMTPConnectError as err:
lookup['message'] = "Internal Error. Maybe blacklisted"
return lookup
示例13: provider_from_address
# 需要导入模块: from dns import resolver [as 别名]
# 或者: from dns.resolver import NoNameservers [as 别名]
def provider_from_address(email_address, dns_resolver=_dns_resolver):
if not EMAIL_REGEX.match(email_address):
raise InvalidEmailAddressError('Invalid email address')
domain = email_address.split('@')[1].lower()
mx_domains = get_mx_domains(domain, dns_resolver)
ns_records = []
try:
ns_records = dns_resolver().query(domain, 'NS')
except NoNameservers:
log.error('NoNameservers', domain=domain)
except NXDOMAIN:
log.error('No such domain', domain=domain)
except Timeout:
log.error('Time out during resolution', domain=domain)
except NoAnswer:
log.error('No answer from provider', domain=domain)
for (name, info) in providers.iteritems():
provider_domains = info.get('domains', [])
# If domain is in the list of known domains for a provider,
# return the provider.
for d in provider_domains:
if domain.endswith(d):
return name
for (name, info) in providers.iteritems():
provider_mx = info.get('mx_servers', [])
# If a retrieved mx_domain is in the list of stored MX domains for a
# provider, return the provider.
if mx_match(mx_domains, provider_mx):
return name
for (name, info) in providers.iteritems():
provider_ns = info.get('ns_servers', [])
# If a retrieved name server is in the list of stored name servers for
# a provider, return the provider.
for rdata in ns_records:
if str(rdata).lower() in provider_ns:
return name
return 'unknown'
# From tornado.httputil