本文整理汇总了Python中twisted.names.dns.DNSDatagramProtocol方法的典型用法代码示例。如果您正苦于以下问题:Python dns.DNSDatagramProtocol方法的具体用法?Python dns.DNSDatagramProtocol怎么用?Python dns.DNSDatagramProtocol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.names.dns
的用法示例。
在下文中一共展示了dns.DNSDatagramProtocol方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_connectedProtocol
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def test_connectedProtocol(self):
"""
L{client.Resolver._connectedProtocol} returns a new
L{DNSDatagramProtocol} connected to a new address with a
cryptographically secure random port number.
"""
resolver = client.Resolver(servers=[('example.com', 53)])
firstProto = resolver._connectedProtocol()
secondProto = resolver._connectedProtocol()
self.assertIsNotNone(firstProto.transport)
self.assertIsNotNone(secondProto.transport)
self.assertNotEqual(
firstProto.transport.getHost().port,
secondProto.transport.getHost().port)
return defer.gatherResults([
defer.maybeDeferred(firstProto.transport.stopListening),
defer.maybeDeferred(secondProto.transport.stopListening)])
示例2: test_protocolShutDown
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [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)
示例3: _connectedProtocol
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def _connectedProtocol(self):
"""
Return a new L{DNSDatagramProtocol} bound to a randomly selected port
number.
"""
if 'protocol' in self.__dict__:
# Some code previously asked for or set the deprecated `protocol`
# attribute, so it probably expects that object to be used for
# queries. Give it back and skip the super awesome source port
# randomization logic. This is actually a really good reason to
# remove this deprecated backward compatibility as soon as
# possible. -exarkun
return self.protocol
proto = dns.DNSDatagramProtocol(self)
while True:
try:
self._reactor.listenUDP(dns.randomSource(), proto)
except error.CannotListenError:
pass
else:
return proto
示例4: makeService
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def makeService(config):
import client, cache, hosts
ca, cl = [], []
if config['cache']:
ca.append(cache.CacheResolver(verbose=config['verbose']))
if config['recursive']:
cl.append(client.createResolver(resolvconf=config['resolv-conf']))
if config['hosts-file']:
cl.append(hosts.Resolver(file=config['hosts-file']))
f = server.DNSServerFactory(config.zones, ca, cl, config['verbose'])
p = dns.DNSDatagramProtocol(f)
f.noisy = 0
ret = service.MultiService()
for (klass, arg) in [(internet.TCPServer, f), (internet.UDPServer, p)]:
s = klass(config['port'], arg, interface=config['interface'])
s.setServiceParent(ret)
for svc in config.svcs:
svc.setServiceParent(ret)
return ret
示例5: test_resolverProtocol
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def test_resolverProtocol(self):
"""
Reading L{client.Resolver.protocol} causes a deprecation warning to be
emitted and evaluates to an instance of L{DNSDatagramProtocol}.
"""
resolver = client.Resolver(servers=[('example.com', 53)])
self.addCleanup(setWarningMethod, getWarningMethod())
warnings = []
setWarningMethod(
lambda message, category, stacklevel:
warnings.append((message, category, stacklevel)))
protocol = resolver.protocol
self.assertIsInstance(protocol, dns.DNSDatagramProtocol)
self.assertEqual(
warnings, [("Resolver.protocol is deprecated; use "
"Resolver.queryUDP instead.",
PendingDeprecationWarning, 0)])
self.assertIdentical(protocol, resolver.protocol)
示例6: test_connectedProtocol
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def test_connectedProtocol(self):
"""
L{client.Resolver._connectedProtocol} returns a new
L{DNSDatagramProtocol} connected to a new address with a
cryptographically secure random port number.
"""
resolver = client.Resolver(servers=[('example.com', 53)])
firstProto = resolver._connectedProtocol()
secondProto = resolver._connectedProtocol()
self.assertNotIdentical(firstProto.transport, None)
self.assertNotIdentical(secondProto.transport, None)
self.assertNotEqual(
firstProto.transport.getHost().port,
secondProto.transport.getHost().port)
return defer.gatherResults([
defer.maybeDeferred(firstProto.transport.stopListening),
defer.maybeDeferred(secondProto.transport.stopListening)])
示例7: test_protocolShutDown
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [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('foo.example.com'))
self.assertFalse(protocols[0].transport.disconnected)
result.callback(dns.Message())
self.assertTrue(protocols[0].transport.disconnected)
示例8: setUpDNS
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def setUpDNS(self):
self.auth = TestAuthority()
factory = server.DNSServerFactory([self.auth])
protocol = dns.DNSDatagramProtocol(factory)
while 1:
self.port = reactor.listenTCP(0, factory, interface='127.0.0.1')
portNumber = self.port.getHost().port
try:
self.udpPort = reactor.listenUDP(portNumber, protocol, interface='127.0.0.1')
except CannotListenError:
self.port.stopListening()
else:
break
self.resolver = client.Resolver(servers=[('127.0.0.1', portNumber)])
示例9: sendReply
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def sendReply(self, protocol, message, address):
"""
Send a response C{message} to a given C{address} via the supplied
C{protocol}.
Message payload will be logged if C{DNSServerFactory.verbose} is C{>1}.
@param protocol: The DNS protocol instance to which to send the message.
@type protocol: L{dns.DNSDatagramProtocol} or L{dns.DNSProtocol}
@param message: The DNS message to be sent.
@type message: L{dns.Message}
@param address: The address to which the message will be sent or L{None}
if C{protocol} is a stream protocol.
@type address: L{tuple} or L{None}
"""
if self.verbose > 1:
s = ' '.join([str(a.payload) for a in message.answers])
auth = ' '.join([str(a.payload) for a in message.authority])
add = ' '.join([str(a.payload) for a in message.additional])
if not s:
log.msg("Replying with no answers")
else:
log.msg("Answers are " + s)
log.msg("Authority is " + auth)
log.msg("Additional is " + add)
if address is None:
protocol.writeMessage(message)
else:
protocol.writeMessage(message, address)
self._verboseLog(
"Processed query in %0.3f seconds" % (
time.time() - message.timeReceived))
示例10: gotResolverError
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def gotResolverError(self, failure, protocol, message, address):
"""
A callback used by L{DNSServerFactory.handleQuery} for handling deferred
errors from C{self.resolver.query}.
Constructs a response message from the original query message by
assigning a suitable error code to C{rCode}.
An error message will be logged if C{DNSServerFactory.verbose} is C{>1}.
@param failure: The reason for the failed resolution (as reported by
C{self.resolver.query}).
@type failure: L{Failure<twisted.python.failure.Failure>}
@param protocol: The DNS protocol instance to which to send a response
message.
@type protocol: L{dns.DNSDatagramProtocol} or L{dns.DNSProtocol}
@param message: The original DNS query message for which a response
message will be constructed.
@type message: L{dns.Message}
@param address: The address to which the response message will be sent
or L{None} if C{protocol} is a stream protocol.
@type address: L{tuple} or L{None}
"""
if failure.check(dns.DomainError, dns.AuthoritativeDomainError):
rCode = dns.ENAME
else:
rCode = dns.ESERVER
log.err(failure)
response = self._responseFromMessage(message=message, rCode=rCode)
self.sendReply(protocol, response, address)
self._verboseLog("Lookup failed")
示例11: handleQuery
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def handleQuery(self, message, protocol, address):
"""
Called by L{DNSServerFactory.messageReceived} when a query message is
received.
Takes the first query from the received message and dispatches it to
C{self.resolver.query}.
Adds callbacks L{DNSServerFactory.gotResolverResponse} and
L{DNSServerFactory.gotResolverError} to the resulting deferred.
Note: Multiple queries in a single message are not supported because
there is no standard way to respond with multiple rCodes, auth,
etc. This is consistent with other DNS server implementations. See
U{http://tools.ietf.org/html/draft-ietf-dnsext-edns1-03} for a proposed
solution.
@param protocol: The DNS protocol instance to which to send a response
message.
@type protocol: L{dns.DNSDatagramProtocol} or L{dns.DNSProtocol}
@param message: The original DNS query message for which a response
message will be constructed.
@type message: L{dns.Message}
@param address: The address to which the response message will be sent
or L{None} if C{protocol} is a stream protocol.
@type address: L{tuple} or L{None}
@return: A C{deferred} which fires with the resolved result or error of
the first query in C{message}.
@rtype: L{Deferred<twisted.internet.defer.Deferred>}
"""
query = message.queries[0]
return self.resolver.query(query).addCallback(
self.gotResolverResponse, protocol, message, address
).addErrback(
self.gotResolverError, protocol, message, address
)
示例12: handleStatus
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def handleStatus(self, message, protocol, address):
"""
Called by L{DNSServerFactory.messageReceived} when a status message is
received.
Replies with a I{Not Implemented} error by default.
An error message will be logged if C{DNSServerFactory.verbose} is C{>1}.
Override in a subclass.
@param protocol: The DNS protocol instance to which to send a response
message.
@type protocol: L{dns.DNSDatagramProtocol} or L{dns.DNSProtocol}
@param message: The original DNS query message for which a response
message will be constructed.
@type message: L{dns.Message}
@param address: The address to which the response message will be sent
or L{None} if C{protocol} is a stream protocol.
@type address: L{tuple} or L{None}
"""
message.rCode = dns.ENOTIMP
self.sendReply(protocol, message, address)
self._verboseLog("Status request from %r" % (address,))
示例13: handleNotify
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def handleNotify(self, message, protocol, address):
"""
Called by L{DNSServerFactory.messageReceived} when a notify message is
received.
Replies with a I{Not Implemented} error by default.
An error message will be logged if C{DNSServerFactory.verbose} is C{>1}.
Override in a subclass.
@param protocol: The DNS protocol instance to which to send a response
message.
@type protocol: L{dns.DNSDatagramProtocol} or L{dns.DNSProtocol}
@param message: The original DNS query message for which a response
message will be constructed.
@type message: L{dns.Message}
@param address: The address to which the response message will be sent
or L{None} if C{protocol} is a stream protocol.
@type address: L{tuple} or L{None}
"""
message.rCode = dns.ENOTIMP
self.sendReply(protocol, message, address)
self._verboseLog("Notify message from %r" % (address,))
示例14: handleOther
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def handleOther(self, message, protocol, address):
"""
Called by L{DNSServerFactory.messageReceived} when a message with
unrecognised I{OPCODE} is received.
Replies with a I{Not Implemented} error by default.
An error message will be logged if C{DNSServerFactory.verbose} is C{>1}.
Override in a subclass.
@param protocol: The DNS protocol instance to which to send a response
message.
@type protocol: L{dns.DNSDatagramProtocol} or L{dns.DNSProtocol}
@param message: The original DNS query message for which a response
message will be constructed.
@type message: L{dns.Message}
@param address: The address to which the response message will be sent
or L{None} if C{protocol} is a stream protocol.
@type address: L{tuple} or L{None}
"""
message.rCode = dns.ENOTIMP
self.sendReply(protocol, message, address)
self._verboseLog(
"Unknown op code (%d) from %r" % (message.opCode, address))
示例15: allowQuery
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import DNSDatagramProtocol [as 别名]
def allowQuery(self, message, protocol, address):
"""
Called by L{DNSServerFactory.messageReceived} to decide whether to
process a received message or to reply with C{dns.EREFUSED}.
This default implementation permits anything but empty queries.
Override in a subclass to implement alternative policies.
@param message: The DNS message that was received.
@type message: L{dns.Message}
@param protocol: The DNS protocol instance which received the message
@type protocol: L{dns.DNSDatagramProtocol} or L{dns.DNSProtocol}
@param address: The address from which the message was received. Only
provided for messages received by datagram protocols. The origin of
Messages received from stream protocols can be gleaned from the
protocol C{transport} attribute.
@type address: L{tuple} or L{None}
@return: L{True} if the received message contained one or more queries,
else L{False}.
@rtype: L{bool}
"""
return len(message.queries)