本文整理汇总了Python中twisted.internet.interfaces.IResolver方法的典型用法代码示例。如果您正苦于以下问题:Python interfaces.IResolver方法的具体用法?Python interfaces.IResolver怎么用?Python interfaces.IResolver使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.interfaces
的用法示例。
在下文中一共展示了interfaces.IResolver方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getResolver
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def getResolver():
"""
Get a Resolver instance.
Create twisted.names.client.theResolver if it is L{None}, and then return
that value.
@rtype: C{IResolver}
"""
global theResolver
if theResolver is None:
try:
theResolver = createResolver()
except ValueError:
theResolver = createResolver(servers=[('127.0.0.1', 53)])
return theResolver
示例2: getResolver
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def getResolver():
"""
Get a Resolver instance.
Create twisted.names.client.theResolver if it is C{None}, and then return
that value.
@rtype: C{IResolver}
"""
global theResolver
if theResolver is None:
try:
theResolver = createResolver()
except ValueError:
theResolver = createResolver(servers=[('127.0.0.1', 53)])
return theResolver
示例3: createResolver
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def createResolver(servers=None, resolvconf=None, hosts=None):
"""
Create and return a Resolver.
@type servers: C{list} of C{(str, int)} or L{None}
@param servers: If not L{None}, interpreted as a list of domain name servers
to attempt to use. Each server is a tuple of address in C{str} dotted-quad
form and C{int} port number.
@type resolvconf: C{str} or L{None}
@param resolvconf: If not L{None}, on posix systems will be interpreted as
an alternate resolv.conf to use. Will do nothing on windows systems. If
L{None}, /etc/resolv.conf will be used.
@type hosts: C{str} or L{None}
@param hosts: If not L{None}, an alternate hosts file to use. If L{None}
on posix systems, /etc/hosts will be used. On windows, C:\windows\hosts
will be used.
@rtype: C{IResolver}
"""
if platform.getType() == 'posix':
if resolvconf is None:
resolvconf = b'/etc/resolv.conf'
if hosts is None:
hosts = b'/etc/hosts'
theResolver = Resolver(resolvconf, servers)
hostResolver = hostsModule.Resolver(hosts)
else:
if hosts is None:
hosts = r'c:\windows\hosts'
from twisted.internet import reactor
bootstrap = _ThreadedResolverImpl(reactor)
hostResolver = hostsModule.Resolver(hosts)
theResolver = root.bootstrap(bootstrap, resolverFactory=Resolver)
L = [hostResolver, cache.CacheResolver(), theResolver]
return resolve.ResolverChain(L)
示例4: test_interface
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def test_interface(self):
"""
L{cache.CacheResolver} implements L{interfaces.IResolver}
"""
verifyClass(interfaces.IResolver, cache.CacheResolver)
示例5: test_resolverBaseProvidesIResolver
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def test_resolverBaseProvidesIResolver(self):
"""
L{ResolverBase} provides the L{IResolver} interface.
"""
verifyClass(IResolver, ResolverBase)
示例6: test_interface
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def test_interface(self):
"""
L{client.getResolver} returns an object providing L{IResolver}.
"""
with AlternateReactor(Clock()):
resolver = client.getResolver()
self.assertTrue(verifyObject(IResolver, resolver))
示例7: test_clientProvidesIResolver
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def test_clientProvidesIResolver(self):
"""
L{client} provides L{IResolver} through a series of free
functions.
"""
verifyObject(IResolver, client)
示例8: test_clientResolverProvidesIResolver
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def test_clientResolverProvidesIResolver(self):
"""
L{client.Resolver} provides L{IResolver}.
"""
verifyClass(IResolver, client.Resolver)
示例9: __init__
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def __init__(self, resolvers):
"""
@type resolvers: L{list}
@param resolvers: A L{list} of L{IResolver} providers.
"""
common.ResolverBase.__init__(self)
self.resolvers = resolvers
示例10: _lookup
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def _lookup(self, name, cls, type, timeout):
"""
Build a L{dns.Query} for the given parameters and dispatch it
to each L{IResolver} in C{self.resolvers} until an answer or
L{error.AuthoritativeDomainError} is returned.
@type name: C{str}
@param name: DNS name to resolve.
@type type: C{int}
@param type: DNS record type.
@type cls: C{int}
@param cls: DNS record class.
@type timeout: Sequence of C{int}
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@rtype: L{Deferred}
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
if not self.resolvers:
return defer.fail(error.DomainError())
q = dns.Query(name, type, cls)
d = self.resolvers[0].query(q, timeout)
for r in self.resolvers[1:]:
d = d.addErrback(
FailureHandler(r.query, q, timeout)
)
return d
示例11: createResolver
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def createResolver(servers=None, resolvconf=None, hosts=None):
"""
Create and return a Resolver.
@type servers: C{list} of C{(str, int)} or C{None}
@param servers: If not C{None}, interpreted as a list of addresses of
domain name servers to attempt to use. Addresses should be in dotted-quad
form.
@type resolvconf: C{str} or C{None}
@param resolvconf: If not C{None}, on posix systems will be interpreted as
an alternate resolv.conf to use. Will do nothing on windows systems. If
C{None}, /etc/resolv.conf will be used.
@type hosts: C{str} or C{None}
@param hosts: If not C{None}, an alternate hosts file to use. If C{None}
on posix systems, /etc/hosts will be used. On windows, C:\windows\hosts
will be used.
@rtype: C{IResolver}
"""
from twisted.names import resolve, cache, root, hosts as hostsModule
if platform.getType() == 'posix':
if resolvconf is None:
resolvconf = '/etc/resolv.conf'
if hosts is None:
hosts = '/etc/hosts'
theResolver = Resolver(resolvconf, servers)
hostResolver = hostsModule.Resolver(hosts)
else:
if hosts is None:
hosts = r'c:\windows\hosts'
from twisted.internet import reactor
bootstrap = _ThreadedResolverImpl(reactor)
hostResolver = hostsModule.Resolver(hosts)
theResolver = root.bootstrap(bootstrap)
L = [hostResolver, cache.CacheResolver(), theResolver]
return resolve.ResolverChain(L)
示例12: reverseResolve
# 需要导入模块: from twisted.internet import interfaces [as 别名]
# 或者: from twisted.internet.interfaces import IResolver [as 别名]
def reverseResolve(
ip: MaybeIPAddress, resolver: IResolver = None
) -> Optional[List[str]]:
"""Using the specified IResolver, reverse-resolves the specifed `ip`.
:return: a sorted list of resolved hostnames (which the specified IP
address reverse-resolves to). If the DNS lookup appeared to succeed,
but no hostnames were found, returns an empty list. If the DNS lookup
timed out or an error occurred, returns None.
"""
if resolver is None:
resolver = getResolver()
ip = IPAddress(ip)
try:
data = yield resolver.lookupPointer(
ip.reverse_dns, timeout=REVERSE_RESOLVE_RETRIES
)
# I love the concise way in which I can ask the Twisted data structure
# what the list of hostnames is. This is great.
results = sorted(
(rr.payload.name.name.decode("idna") for rr in data[0]),
key=preferred_hostnames_sort_key,
)
except AuthoritativeDomainError:
# "Failed to reverse-resolve '%s': authoritative failure." % ip
# This means the name didn't resolve, so return an empty list.
return []
except DomainError:
# "Failed to reverse-resolve '%s': no records found." % ip
# This means the name didn't resolve, so return an empty list.
return []
except DNSQueryTimeoutError:
# "Failed to reverse-resolve '%s': timed out." % ip
# Don't return an empty list since this implies a temporary failure.
pass
except ResolverError:
# "Failed to reverse-resolve '%s': rejected by local resolver." % ip
# Don't return an empty list since this could be temporary (unclear).
pass
else:
return results
return None