本文整理汇总了Python中dns.query方法的典型用法代码示例。如果您正苦于以下问题:Python dns.query方法的具体用法?Python dns.query怎么用?Python dns.query使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dns
的用法示例。
在下文中一共展示了dns.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _hostname_matches_additional
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def _hostname_matches_additional(self, ip, name, additional):
"""
Search for *name* in *additional* and if it is found, check that it
includes *ip*.
:param ip: The IP address to search for.
:type ip: :py:class:`ipaddress.IPv4Address`, :py:class:`ipaddress.IPv6Address`
:param str name: The name to search for.
:param tuple additional: The additional data returned from a dns query to search in.
:return: The first value is whether or not *name* was found in *additional*, the second is if *ip* was also found.
:rtype: tuple
"""
rdtype = (1 if isinstance(ip, ipaddress.IPv4Address) else 28)
ip = str(ip)
additional = (entry for entry in additional if entry.rdtype == rdtype)
entry = next((entry for entry in additional if str(entry.name)[:-1] == name), None)
if entry is None:
return False, None
item = next((item for item in entry.items if item.address == ip), None)
return True, item is not None
示例2: transfer_info
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def transfer_info(self):
ret_zones = list()
try:
nss = dns.resolver.query(self.domain, 'NS')
nameservers = [str(ns) for ns in nss]
ns_addr = dns.resolver.query(nameservers[0], 'A')
# dnspython 的 bug,需要设置 lifetime 参数
zones = dns.zone.from_xfr(dns.query.xfr(ns_addr, self.domain, relativize=False, timeout=2, lifetime=2), check_origin=False)
names = zones.nodes.keys()
for n in names:
subdomain = ''
for t in range(0, len(n) - 1):
if subdomain != '':
subdomain += '.'
subdomain += str(n[t].decode())
if subdomain != self.domain:
ret_zones.append(subdomain)
return ret_zones
except BaseException:
return []
示例3: get_a
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def get_a(self, host_trg):
"""
Function for resolving the A Record for a given host. Returns an Array of
the IP Address it resolves to. It will also return CNAME data.
"""
address = []
try:
ipv4_answers = self._res.query(host_trg, 'A')
for ardata in ipv4_answers.response.answer:
for rdata in ardata:
if rdata.rdtype == 5:
if rdata.target.to_text().endswith('.'):
address.append(["CNAME", host_trg, rdata.target.to_text()[:-1]])
host_trg = rdata.target.to_text()[:-1]
else:
address.append(["CNAME", host_trg, rdata.target.to_text()])
host_trg = rdata.target.to_text()
else:
address.append(["A", host_trg, rdata.address])
except:
return address
return address
示例4: get_aaaa
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def get_aaaa(self, host_trg):
"""
Function for resolving the AAAA Record for a given host. Returns an Array of
the IP Address it resolves to. It will also return CNAME data.
"""
address = []
try:
ipv6_answers = self._res.query(host_trg, 'AAAA')
for ardata in ipv6_answers.response.answer:
for rdata in ardata:
if rdata.rdtype == 5:
if rdata.target.to_text().endswith('.'):
address.append(["CNAME", host_trg, rdata.target.to_text()[:-1]])
host_trg = rdata.target.to_text()[:-1]
else:
address.append(["CNAME", host_trg, rdata.target.to_text()])
host_trg = rdata.target.to_text()
else:
address.append(["AAAA", host_trg, rdata.address])
except:
return address
return address
示例5: get_spf
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def get_spf(self):
"""
Function for SPF Record resolving returns the string with the SPF definition.
Prints the string for the SPF Record and Returns the string
"""
spf_record = []
try:
answers = self._res.query(self._domain, 'SPF')
for rdata in answers:
name = ''.join(rdata.strings)
spf_record.append(['SPF', name])
except:
return None
return spf_record
示例6: get_txt
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def get_txt(self, target=None):
"""
Function for TXT Record resolving returns the string.
"""
txt_record = []
if target is None:
target = self._domain
try:
answers = self._res.query(target, 'TXT')
for rdata in answers:
string = "".join(rdata.strings)
txt_record.append(['TXT', target, string])
except:
return []
return txt_record
示例7: get_ptr
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def get_ptr(self, ipaddress):
"""
Function for resolving PTR Record given it's IPv4 or IPv6 Address.
"""
found_ptr = []
n = dns.reversename.from_address(ipaddress)
try:
answers = self._res.query(n, 'PTR')
for a in answers:
if a.target.to_text().endswith('.'):
found_ptr.append(['PTR', a.target.to_text()[:-1], ipaddress])
else:
found_ptr.append(['PTR', a.target.to_text(), ipaddress])
return found_ptr
except:
return None
示例8: test_send_notify_message
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def test_send_notify_message(self):
# id 10001
# opcode NOTIFY
# rcode NOERROR
# flags QR AA
# ;QUESTION
# example.com. IN SOA
# ;ANSWER
# ;AUTHORITY
# ;ADDITIONAL
expected_notify_response = ("2711a4000001000000000000076578616d706c650"
"3636f6d0000060001")
context = self.get_context()
with patch.object(dns.query, 'udp', return_value=dns.message.from_wire(
binascii.a2b_hex(expected_notify_response))):
response, retry = self.notify.notify_zone_changed(
context, objects.Zone.from_dict(self.test_zone),
self.nameserver.host, self.nameserver.port, 0, 0, 2, 0)
self.assertEqual(response, dns.message.from_wire(
binascii.a2b_hex(expected_notify_response)))
self.assertEqual(retry, 1)
示例9: test_send_notify_message_non_auth
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def test_send_notify_message_non_auth(self):
# id 10001
# opcode NOTIFY
# rcode NOTAUTH
# flags QR
# ;QUESTION
# example.com. IN SOA
# ;ANSWER
# ;AUTHORITY
# ;ADDITIONAL
non_auth_notify_response = ("2711a4090001000000000000076578616d706c650"
"3636f6d0000060001")
context = self.get_context()
with patch.object(dns.query, 'udp', return_value=dns.message.from_wire(
binascii.a2b_hex(non_auth_notify_response))):
response, retry = self.notify.notify_zone_changed(
context, objects.Zone.from_dict(self.test_zone),
self.nameserver.host, self.nameserver.port, 0, 0, 2, 0)
self.assertIsNone(response)
self.assertEqual(retry, 1)
示例10: test_poll_for_serial_number
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def test_poll_for_serial_number(self):
# id 10001
# opcode QUERY
# rcode NOERROR
# flags QR AA
# ;QUESTION
# example.com. IN SOA
# ;ANSWER
# example.com. 3600 IN SOA example-ns.com. admin.example.com. 100 3600
# 600 86400 3600
# ;AUTHORITY
# ;ADDITIONAL
poll_response = ("271184000001000100000000076578616d706c6503636f6d0000"
"060001c00c0006000100000e1000290a6578616d706c652d6e73"
"c0140561646d696ec00c0000006400000e100000025800015180"
"00000e10")
context = self.get_context()
with patch.object(dns.query, 'udp', return_value=dns.message.from_wire(
binascii.a2b_hex(poll_response))):
status, serial, retries = self.notify.get_serial_number(
context, objects.Zone.from_dict(self.test_zone),
self.nameserver.host, self.nameserver.port, 0, 0, 2, 0)
self.assertEqual(status, 'SUCCESS')
self.assertEqual(serial, self.test_zone['serial'])
self.assertEqual(retries, 2)
示例11: test_poll_for_serial_number_lower_serial
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def test_poll_for_serial_number_lower_serial(self):
# id 10001
# opcode QUERY
# rcode NOERROR
# flags QR AA
# ;QUESTION
# example.com. IN SOA
# ;ANSWER
# example.com. 3600 IN SOA example-ns.com. admin.example.com. 99 3600
# 600 86400 3600
# ;AUTHORITY
# ;ADDITIONAL
poll_response = ("271184000001000100000000076578616d706c6503636f6d0000"
"060001c00c0006000100000e1000290a6578616d706c652d6e73"
"c0140561646d696ec00c0000006300000e100000025800015180"
"00000e10")
context = self.get_context()
with patch.object(dns.query, 'udp', return_value=dns.message.from_wire(
binascii.a2b_hex(poll_response))):
status, serial, retries = self.notify.get_serial_number(
context, objects.Zone.from_dict(self.test_zone),
self.nameserver.host, self.nameserver.port, 0, 0, 2, 0)
self.assertEqual(status, 'ERROR')
self.assertEqual(serial, 99)
self.assertEqual(retries, 0)
示例12: test_poll_for_serial_number_higher_serial
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def test_poll_for_serial_number_higher_serial(self):
# id 10001
# opcode QUERY
# rcode NOERROR
# flags QR AA
# ;QUESTION
# example.com. IN SOA
# ;ANSWER
# example.com. 3600 IN SOA example-ns.com. admin.example.com. 101 3600
# 600 86400 3600
# ;AUTHORITY
# ;ADDITIONAL
poll_response = ("271184000001000100000000076578616d706c6503636f6d0000"
"060001c00c0006000100000e1000290a6578616d706c652d6e73"
"c0140561646d696ec00c0000006500000e100000025800015180"
"00000e10")
context = self.get_context()
with patch.object(dns.query, 'udp', return_value=dns.message.from_wire(
binascii.a2b_hex(poll_response))):
status, serial, retries = self.notify.get_serial_number(
context, objects.Zone.from_dict(self.test_zone),
self.nameserver.host, self.nameserver.port, 0, 0, 2, 0)
self.assertEqual(status, 'SUCCESS')
self.assertEqual(serial, 101)
self.assertEqual(retries, 2)
示例13: query
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def query(resolver, domain, record_type='A', tcp=False):
try:
resp = resolver.query(domain, record_type, raise_on_no_answer=False, tcp=tcp)
if resp.response.answer:
return resp
# If we don't receive an answer from our current resolver let's
# assume we received information on nameservers we can use and
# perform the same query with those nameservers
if resp.response.additional and resp.response.authority:
ns = [
rdata.address
for additionals in resp.response.additional
for rdata in additionals.items
]
resolver.nameservers = ns
return query(resolver, domain, record_type, tcp=tcp)
return None
except (dns.resolver.NXDOMAIN, dns.resolver.NoNameservers, dns.exception.Timeout):
return None
示例14: _has_dns_propagated
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def _has_dns_propagated(fqdn, token):
txt_records = []
try:
dns_resolver = dns.resolver.Resolver()
dns_resolver.nameservers = [get_authoritative_nameserver(fqdn)]
dns_response = dns_resolver.query(fqdn, "TXT")
for rdata in dns_response:
for txt_record in rdata.strings:
txt_records.append(txt_record.decode("utf-8"))
except dns.exception.DNSException:
metrics.send("has_dns_propagated_fail", "counter", 1, metric_tags={"dns": fqdn})
return False
for txt_record in txt_records:
if txt_record == token:
metrics.send("has_dns_propagated_success", "counter", 1, metric_tags={"dns": fqdn})
return True
return False
示例15: get
# 需要导入模块: import dns [as 别名]
# 或者: from dns import query [as 别名]
def get(self, key):
"""Get the answer associated with I{key}. Returns None if
no answer is cached for the key.
@param key: the key
@type key: (dns.name.Name, int, int) tuple whose values are the
query name, rdtype, and rdclass.
@rtype: dns.resolver.Answer object or None
"""
try:
self.lock.acquire()
self._maybe_clean()
v = self.data.get(key)
if v is None or v.expiration <= time.time():
return None
return v
finally:
self.lock.release()