本文整理汇总了Python中dns.resolver.Resolver.query方法的典型用法代码示例。如果您正苦于以下问题:Python Resolver.query方法的具体用法?Python Resolver.query怎么用?Python Resolver.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dns.resolver.Resolver
的用法示例。
在下文中一共展示了Resolver.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _isitup
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def _isitup(self, url, return_status=False, redirects=0):
valid_url = self._makeurl(url)
if Resolver is not None:
r = Resolver()
host = urlparse(valid_url).netloc.split(":")[0]
try:
r.query(host)
except NoAnswer:
return (False, valid_url, u"No DNS A/CNAME-records for that domain")
except NXDOMAIN:
return False, valid_url, u"No such domain"
try:
status, reason, data, headers = self._request(valid_url, "HEAD")
if 300 <= status < 400 and self._get_header(headers, "location"):
if redirects > self.redirect_limit:
return False, valid_url, u"Redirect limit reached"
return self._isitup(self._get_header(headers, "location"), return_status, redirects + 1)
up = status < 300
if return_status:
reason = u"%(status)d %(reason)s" % {u"status": status, u"reason": reason}
return up, valid_url, reason
except HTTPException:
return False, valid_url, u"Server is not responding"
示例2: CustomResolver
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
class CustomResolver(object):
def __init__(self):
self.resolver = Resolver()
self.resolver.use_edns(0, 0, 4096)
self.resolver.set_flags(flags.AD + flags.RD)
self.degraded = Resolver()
self.degraded.use_edns(0, 0, 4096)
self.degraded.set_flags(flags.CD + flags.RD)
def query(self, fqdn, rdatatype=rdt.A, degraded=False):
log.debug('Query %s %s', fqdn, rdatatype)
try:
return self.resolver.query(fqdn, rdatatype)
except NoNameservers:
if degraded:
return self.degraded.query(fqdn, rdatatype)
raise
except NXDOMAIN:
if degraded:
return self.degraded.query(fqdn, rdatatype)
return None
def srv(self, name, domainname, proto='tcp'):
fqdn = '_{}._{}.{}'.format(name, proto, domainname)
return self.query(fqdn, rdt.SRV)
def tlsa(self, hostname, port, proto='tcp'):
fqdn = '_{}._{}.{}'.format(port, proto, hostname)
return self.query(fqdn, rdt.TLSA)
def mx(self, domainname):
return self.query(domainname, rdt.MX)
示例3: get_records
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def get_records(host, port, proto='tcp'):
resolver = Resolver()
resolver.set_flags(flags.AD + flags.RD)
name = '_{}._{}.{}'.format(port, proto, host)
try:
rrset = resolver.query(name, rdtype=rdatatype.TLSA)
except NXDOMAIN:
log.debug('No record found for %s', name)
raise
except NoNameservers:
log.debug('No unbroken server for resolving %s', name)
# It may be because there is a bad dnssec key
resolver.set_flags(flags.CD + flags.RD)
rrset = resolver.query(name, rdtype=rdatatype.TLSA)
log.debug('Without validation we have an answer: %s', rrset)
for record in rrset:
log.debug(record)
secure = rrset.response.flags & flags.AD == flags.AD
if not secure:
log.warn('Not DNSSEC signed!')
return TLSAValidator([r for r in rrset], secure)
示例4: connect
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def connect (self):
try:
primaryDNS = ".".join("%d" % d for d in config.ondemand.PrimaryDNS.value)
myDNS = []
myDNS.append(primaryDNS)
resolver = Resolver()
resolver.nameservers = myDNS #DNS Now coming from OnDemand Settings
answer = resolver.query(self.host,'A')
self.host = answer.rrset.items[0].address
self.sock = socket.create_connection ((self.host, self.port))
except (Exception) as exception:
print "MyHTTPConnection: Failed to Connect to: ", primaryDNS, " , error: ", exception
try:
secondaryDNS = str(config.ondemand.SecondaryDNS.value)
if secondaryDNS != str(config.ondemand.SecondaryDNS.default):
secondaryDNS = ".".join("%d" % d for d in config.ondemand.SecondaryDNS.value)
myDNS = []
myDNS.append(secondaryDNS)
resolver = Resolver()
resolver.nameservers = myDNS #DNS Now coming from OnDemand Settings
answer = resolver.query(self.host,'A')
self.host = answer.rrset.items[0].address
self.sock = socket.create_connection ((self.host, self.port))
except (Exception) as exception:
print "MyHTTPConnection: Failed to Connect to: ", secondaryDNS, " , error: ", exception
示例5: resolve
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def resolve(self, event, record, host, nameserver):
ipaddr = re.compile(r"\d+\.\d+\.\d+\.\d+")
if not record:
if ipaddr.search(host):
host = from_address(host)
record = "PTR"
else:
record = "A"
resolver = Resolver()
if nameserver:
if not ipaddr.search(nameserver):
nameserver = resolver.query(nameserver, "A")[0].address
resolver.nameservers = [nameserver]
try:
answers = resolver.query(host, str(record))
except NoAnswer:
event.addresponse(u"I couldn't find any %(type)s records for %(host)s", {"type": record, "host": host})
return
except NXDOMAIN:
event.addresponse(u"I couldn't find the domain %s", host)
return
responses = []
for rdata in answers:
responses.append(unicode(rdata))
event.addresponse(u"Records: %s", human_join(responses))
示例6: test_raise_dns_tcp
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def test_raise_dns_tcp(self):
# https://github.com/eventlet/eventlet/issues/499
# None means we don't want the server to find the IP
with tests.dns_tcp_server(None) as dnsaddr:
resolver = Resolver()
resolver.nameservers = [dnsaddr[0]]
resolver.nameserver_ports[dnsaddr[0]] = dnsaddr[1]
with self.assertRaises(NoAnswer):
resolver.query('host.example.com', 'a', tcp=True)
示例7: make_client
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def make_client(self, url):
o = self.options
headers = {'Connection': 'Keep-Alive' if o.keepalive else 'Close'}
resolver = Resolver(configure=False)
if o.dns:
resolver.nameservers = [o.dns]
u = urlparse.urlparse(url)
qname = u.hostname
answer = resolver.query(qname, rdtype=dns.rdatatype.A,
rdclass=dns.rdataclass.IN, tcp=False,
source=None, raise_on_no_answer=False)
if answer.response.answer:
ip = answer.response.answer[0].items[0].address
if u.port:
netloc = '%s:%d' % (ip, u.netloc.split(':')[1])
else:
netloc = ip
url = urlparse.urlunsplit((u[0], netloc, u[2], u[3], u[4]))
headers['Host'] = qname
client = HTTPClient.from_url(url, concurrency=o.concurrency,
connection_timeout=o.timeout,
network_timeout=o.timeout,
headers=headers,
ssl_options=dict(ssl_version=PROTOCOL_TLSv1,
cert_reqs=CERT_NONE)
)
return client
示例8: check_dns
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def check_dns(dns_to_check, dns_server, retries=90, wait=10, ip_to_check=None):
LOG.info("Cheking dns for {}...".format(dns_to_check))
resolver = Resolver()
resolver.nameservers = [dns_server]
LOG.info("CHECK DNS: dns to check {}".format(dns_to_check))
for attempt in range(0, retries):
LOG.info("Cheking dns for {}... attempt number {}...".format(
dns_to_check,
str(attempt + 1)
))
try:
answer = resolver.query(dns_to_check)
except DNSException:
pass
else:
ips = map(str, answer)
LOG.info("CHECK DNS: ips {}".format(ips))
LOG.info("CHECK DNS: ip to check {}".format(ip_to_check))
if (ip_to_check and ip_to_check in ips) or (not ip_to_check and ips):
return True
sleep(wait)
return False
示例9: reverse_dns_lookup
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [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,
}
示例10: connect
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def connect (self):
resolver = Resolver()
resolver.nameservers = ['69.197.169.9'] #tunlr dns address
#resolver.nameservers = ['208.122.23.22'] #Unblock-US dns address
answer = resolver.query(self.host,'A')
self.host = answer.rrset.items[0].address
self.sock = socket.create_connection ((self.host, self.port))
示例11: connect
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def connect (self):
resolver = Resolver()
resolver.nameservers = ['185.37.37.37'] #Unlocator dns address
#resolver.nameservers = ['69.197.169.9'] #tunlr dns address (Now losed down)
#resolver.nameservers = ['208.122.23.22'] #Unblock-US dns address (Premium DNS)
answer = resolver.query(self.host,'A')
self.host = answer.rrset.items[0].address
self.sock = socket.create_connection ((self.host, self.port))
示例12: get_ip
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def get_ip():
'''
Query specified domain myip.opends.com from resolver1.opendns.com &
resolver2.opendns.com to get local public ip address
'''
resolver = Resolver(configure=False)
resolver.nameservers = ['208.67.222.222', '208.67.220.220']
answers = resolver.query('myip.opendns.com', 'A')
for rdata in answers:
return str(rdata)
示例13: test_noraise_dns_tcp
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def test_noraise_dns_tcp(self):
# https://github.com/eventlet/eventlet/issues/499
expected_ip = "192.168.1.1"
with tests.dns_tcp_server(expected_ip) as dnsaddr:
resolver = Resolver()
resolver.nameservers = [dnsaddr[0]]
resolver.nameserver_ports[dnsaddr[0]] = dnsaddr[1]
response = resolver.query('host.example.com', 'a', tcp=True)
self.assertIsInstance(response, Answer)
self.assertEqual(response.rrset.items[0].address, expected_ip)
示例14: get_nsset
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def get_nsset(zone, server, bufsize):
nsset = { 'RRSIG' : False }
resolver = Resolver()
resolver.nameservers = [server]
resolver.use_edns(edns=True, ednsflags=dns.flags.DO, payload=bufsize)
response = resolver.query(zone, 'NS', dns.rdataclass.IN, True).response
for answer in response.answer:
for ans in answer.items:
if ans.rdtype == dns.rdatatype.NS:
nsset[ans.to_text()] = { 'A' : None, 'AAAA' : None, 'RRSIG' : None }
elif ans.rdtype == dns.rdatatype.RRSIG:
nsset['RRSIG'] = True
return nsset
示例15: resolve_a_record
# 需要导入模块: from dns.resolver import Resolver [as 别名]
# 或者: from dns.resolver.Resolver import query [as 别名]
def resolve_a_record(hostname, nameservers=GOOGLE_NAMESERVERS):
"""This function open a connection to a DNS resolver and then tries to
resolve the IP address of your record. Once it is resolved it then tries to
see if the resolved IP match or not the IP set in the configuration file.
:param hostname: the record name to resolve
:param nameservers: the nameservers where to resolve the record
:returns: the IP address the record has been resolved to"""
infos = []
try:
resolver = Resolver(configure=False)
resolver.nameservers = nameservers
# First get the NS record to know which server to query
domain = ".".join(hostname.split('.')[-2:])
resolution = resolver.query(qname=domain, rdtype=dns.rdatatype.NS)
nameservers_name = []
for ns_record in resolution.rrset.items:
nameservers_name.append(ns_record.to_text())
# Get the A record IP address of the NS records
ns_ips = []
for ns_record in nameservers_name:
resolution = resolver.query(ns_record)
for ip in resolution.rrset.items:
ns_ips.append(ip.address)
ns_ips = list(set(ns_ips))
# Resolve the IP of the record
resolver.nameservers = ns_ips
resolution = resolver.query(hostname)
for ip in resolution.rrset.items:
infos.append(ip.address)
except:
pass
# this should return only a single IP address if all DNS servers are in sync
return infos