本文整理汇总了Python中twisted.names.dns.Query方法的典型用法代码示例。如果您正苦于以下问题:Python dns.Query方法的具体用法?Python dns.Query怎么用?Python dns.Query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.names.dns
的用法示例。
在下文中一共展示了dns.Query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_query
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_query(self):
"""
L{dns.Query.encode} returns a byte string representing the fields of the
query which can be decoded into a new L{dns.Query} instance using
L{dns.Query.decode}.
"""
for n in self.names:
for dnstype in range(1, 17):
for dnscls in range(1, 5):
# encode the query
f = BytesIO()
dns.Query(n, dnstype, dnscls).encode(f)
# decode the result
f.seek(0, 0)
result = dns.Query()
result.decode(f)
self.assertEqual(result.name.name, n)
self.assertEqual(result.type, dnstype)
self.assertEqual(result.cls, dnscls)
示例2: test_simpleQuery
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_simpleQuery(self):
"""
Test content received after a query.
"""
d = self.proto.query([dns.Query(b'foo')])
self.assertEqual(len(self.proto.liveMessages.keys()), 1)
m = dns.Message()
m.id = next(iter(self.proto.liveMessages.keys()))
m.answers = [dns.RRHeader(payload=dns.Record_A(address='1.2.3.4'))]
def cb(result):
self.assertEqual(result.answers[0].payload.dottedQuad(), '1.2.3.4')
d.addCallback(cb)
s = m.toStr()
s = struct.pack('!H', len(s)) + s
self.proto.dataReceived(s)
return d
示例3: bytes
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def bytes(cls):
"""
Bytes which are expected when encoding an instance constructed using
C{kwargs} and which are expected to result in an identical instance when
decoded.
@return: The L{bytes} of a wire encoded message.
"""
return (
b'\x01\x00' # ID: 256
b'\x04' # QR: 0, OPCODE: 0, AA: 1, TC: 0, RD: 0
b'\x00' # RA: 0, Z, RCODE: 0
b'\x00\x00' # Query count
b'\x00\x01' # Answer count
b'\x00\x00' # Authorities count
b'\x00\x00' # Additionals count
# Answer
b'\x00' # RR NAME (root)
b'\x00\x01' # RR TYPE 1 (A)
b'\x00\x01' # RR CLASS 1 (IN)
b'\x00\x00\x00\x00' # RR TTL
b'\x00\x04' # RDLENGTH 4
b'\x01\x02\x03\x04' # IPv4 1.2.3.4
)
示例4: kwargs
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def kwargs(cls):
"""
Keyword constructor arguments which are expected to result in an
instance which returns C{bytes} when encoded.
@return: A L{dict} of keyword arguments.
"""
return dict(
id=0,
answer=0,
opCode=dns.OP_QUERY,
auth=0,
recDes=0,
recAv=0,
rCode=0,
ednsVersion=3,
dnssecOK=False,
queries=[dns.Query(b'www.example.com', dns.A)],
additional=[])
示例5: test_constructorExpires
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_constructorExpires(self):
"""
Cache entries passed into L{cache.CacheResolver.__init__} get
cancelled just like entries added with cacheResult
"""
r = ([dns.RRHeader(b"example.com", dns.A, dns.IN, 60,
dns.Record_A("127.0.0.1", 60))],
[dns.RRHeader(b"example.com", dns.A, dns.IN, 50,
dns.Record_A("127.0.0.1", 50))],
[dns.RRHeader(b"example.com", dns.A, dns.IN, 40,
dns.Record_A("127.0.0.1", 40))])
clock = task.Clock()
query = dns.Query(name=b"example.com", type=dns.A, cls=dns.IN)
c = cache.CacheResolver({ query : (clock.seconds(), r)}, reactor=clock)
# 40 seconds is enough to expire the entry because expiration is based
# on the minimum TTL.
clock.advance(40)
self.assertNotIn(query, c.cache)
return self.assertFailure(
c.lookupAddress(b"example.com"), dns.DomainError)
示例6: test_expiredTTLLookup
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_expiredTTLLookup(self):
"""
When the cache is queried exactly as the cached entry should expire but
before it has actually been cleared, the cache does not return the
expired entry.
"""
r = ([dns.RRHeader(b"example.com", dns.A, dns.IN, 60,
dns.Record_A("127.0.0.1", 60))],
[dns.RRHeader(b"example.com", dns.A, dns.IN, 50,
dns.Record_A("127.0.0.1", 50))],
[dns.RRHeader(b"example.com", dns.A, dns.IN, 40,
dns.Record_A("127.0.0.1", 40))])
clock = task.Clock()
# Make sure timeouts never happen, so entries won't get cleared:
clock.callLater = lambda *args, **kwargs: None
c = cache.CacheResolver({
dns.Query(name=b"example.com", type=dns.A, cls=dns.IN) :
(clock.seconds(), r)}, reactor=clock)
clock.advance(60.1)
return self.assertFailure(
c.lookupAddress(b"example.com"), dns.DomainError)
示例7: test_multipleSequentialRequests
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_multipleSequentialRequests(self):
"""
After a response is received to a query issued with
L{client.Resolver.query}, another query with the same parameters
results in a new network request.
"""
protocol = StubDNSDatagramProtocol()
resolver = client.Resolver(servers=[('example.com', 53)])
resolver._connectedProtocol = lambda: protocol
queries = protocol.queries
query = dns.Query(b'foo.example.com', dns.A)
# The first query should be passed to the underlying protocol.
resolver.query(query)
self.assertEqual(len(queries), 1)
# Deliver the response.
queries.pop()[-1].callback(dns.Message())
# Repeating the first query should touch the protocol again.
resolver.query(query)
self.assertEqual(len(queries), 1)
示例8: test_multipleConcurrentFailure
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_multipleConcurrentFailure(self):
"""
If the result of a request is an error response, the Deferreds for all
concurrently issued requests associated with that result fire with the
L{Failure}.
"""
protocol = StubDNSDatagramProtocol()
resolver = client.Resolver(servers=[('example.com', 53)])
resolver._connectedProtocol = lambda: protocol
queries = protocol.queries
query = dns.Query(b'foo.example.com', dns.A)
firstResult = resolver.query(query)
secondResult = resolver.query(query)
class ExpectedException(Exception):
pass
queries.pop()[-1].errback(failure.Failure(ExpectedException()))
return defer.gatherResults([
self.assertFailure(firstResult, ExpectedException),
self.assertFailure(secondResult, ExpectedException)])
示例9: test_differentProtocol
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_differentProtocol(self):
"""
L{client.Resolver._connectedProtocol} is called once each time a UDP
request needs to be issued and the resulting protocol instance is used
for that request.
"""
resolver = client.Resolver(servers=[('example.com', 53)])
protocols = []
class FakeProtocol(object):
def __init__(self):
self.transport = StubPort()
def query(self, address, query, timeout=10, id=None):
protocols.append(self)
return defer.succeed(dns.Message())
resolver._connectedProtocol = FakeProtocol
resolver.query(dns.Query(b'foo.example.com'))
resolver.query(dns.Query(b'bar.example.com'))
self.assertEqual(len(set(protocols)), 2)
示例10: test_differentProtocolAfterTimeout
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_differentProtocolAfterTimeout(self):
"""
When a query issued by L{client.Resolver.query} times out, the retry
uses a new protocol instance.
"""
resolver = client.Resolver(servers=[('example.com', 53)])
protocols = []
results = [defer.fail(failure.Failure(DNSQueryTimeoutError(None))),
defer.succeed(dns.Message())]
class FakeProtocol(object):
def __init__(self):
self.transport = StubPort()
def query(self, address, query, timeout=10, id=None):
protocols.append(self)
return results.pop(0)
resolver._connectedProtocol = FakeProtocol
resolver.query(dns.Query(b'foo.example.com'))
self.assertEqual(len(set(protocols)), 2)
示例11: test_singleTCPQueryErrbackOnConnectionFailure
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_singleTCPQueryErrbackOnConnectionFailure(self):
"""
The deferred returned by L{client.Resolver.queryTCP} will
errback when the TCP connection attempt fails. The reason for
the connection failure is passed as the argument to errback.
"""
reactor = proto_helpers.MemoryReactor()
resolver = client.Resolver(
servers=[('192.0.2.100', 53)],
reactor=reactor)
d = resolver.queryTCP(dns.Query('example.com'))
host, port, factory, timeout, bindAddress = reactor.tcpClients[0]
class SentinelException(Exception):
pass
factory.clientConnectionFailed(
reactor.connectors[0], failure.Failure(SentinelException()))
self.failureResultOf(d, SentinelException)
示例12: test_multipleTCPQueryErrbackOnConnectionFailure
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_multipleTCPQueryErrbackOnConnectionFailure(self):
"""
All pending L{resolver.queryTCP} C{deferred}s will C{errback}
with the same C{Failure} if the connection attempt fails.
"""
reactor = proto_helpers.MemoryReactor()
resolver = client.Resolver(
servers=[('192.0.2.100', 53)],
reactor=reactor)
d1 = resolver.queryTCP(dns.Query('example.com'))
d2 = resolver.queryTCP(dns.Query('example.net'))
host, port, factory, timeout, bindAddress = reactor.tcpClients[0]
class SentinelException(Exception):
pass
factory.clientConnectionFailed(
reactor.connectors[0], failure.Failure(SentinelException()))
f1 = self.failureResultOf(d1, SentinelException)
f2 = self.failureResultOf(d2, SentinelException)
self.assertIs(f1, f2)
示例13: bytes
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def bytes(cls):
"""
Bytes which are expected when encoding an instance constructed using
C{kwargs} and which are expected to result in an identical instance when
decoded.
@return: The L{bytes} of a wire encoded message.
"""
return (
b'\x01\x00' # ID 256
b'\x00' # QR: 0, OPCODE: 0, AA: 0, TC: 0, RD: 0
b'\x00' # RA: 0, Z, RCODE: 0
b'\x00\x00' # Query count
b'\x00\x01' # Answer count
b'\x00\x00' # Authorities count
b'\x00\x00' # Additionals count
# Answer
b'\x00' # RR NAME (root)
b'\x00\x01' # RR TYPE 1 (A)
b'\x00\x01' # RR CLASS 1 (IN)
b'\x00\x00\x00\x00' # RR TTL
b'\x00\x04' # RDLENGTH 4
b'\x01\x02\x03\x04' # IPv4 1.2.3.4
)
示例14: test_cachedResultExpires
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_cachedResultExpires(self):
"""
Once the TTL has been exceeded, the result is removed from the cache.
"""
r = ([dns.RRHeader(b"example.com", dns.A, dns.IN, 60,
dns.Record_A("127.0.0.1", 60))],
[dns.RRHeader(b"example.com", dns.A, dns.IN, 50,
dns.Record_A("127.0.0.1", 50))],
[dns.RRHeader(b"example.com", dns.A, dns.IN, 40,
dns.Record_A("127.0.0.1", 40))])
clock = task.Clock()
c = cache.CacheResolver(reactor=clock)
query = dns.Query(name=b"example.com", type=dns.A, cls=dns.IN)
c.cacheResult(query, r)
clock.advance(40)
self.assertNotIn(query, c.cache)
return self.assertFailure(
c.lookupAddress(b"example.com"), dns.DomainError)
示例15: test_protocolShutDown
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import Query [as 别名]
def test_protocolShutDown(self):
"""
After the L{Deferred} returned by L{DNSDatagramProtocol.query} is
called back, the L{DNSDatagramProtocol} is disconnected from its
transport.
"""
resolver = client.Resolver(servers=[('example.com', 53)])
protocols = []
result = defer.Deferred()
class FakeProtocol(object):
def __init__(self):
self.transport = StubPort()
def query(self, address, query, timeout=10, id=None):
protocols.append(self)
return result
resolver._connectedProtocol = FakeProtocol
resolver.query(dns.Query(b'foo.example.com'))
self.assertFalse(protocols[0].transport.disconnected)
result.callback(dns.Message())
self.assertTrue(protocols[0].transport.disconnected)