本文整理汇总了Python中twisted.names.dns.SRV属性的典型用法代码示例。如果您正苦于以下问题:Python dns.SRV属性的具体用法?Python dns.SRV怎么用?Python dns.SRV使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类twisted.names.dns
的用法示例。
在下文中一共展示了dns.SRV属性的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _cbGotServers
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def _cbGotServers(self, result):
answers, auth, add = result
if len(answers) == 1 and answers[0].type == dns.SRV \
and answers[0].payload \
and answers[0].payload.target == dns.Name(b'.'):
# decidedly not available
raise error.DNSLookupError("Service %s not available for domain %s."
% (repr(self.service), repr(self.domain)))
self.servers = []
self.orderedServers = []
for a in answers:
if a.type != dns.SRV or not a.payload:
continue
self.orderedServers.append(a.payload)
示例2: __init__
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def __init__(self, reactor, service, domain, factory,
protocol='tcp', connectFuncName='connectTCP',
connectFuncArgs=(),
connectFuncKwArgs={},
defaultPort=None,
):
"""
@param domain: The domain to connect to. If passed as a unicode
string, it will be encoded using C{idna} encoding.
@type domain: L{bytes} or L{unicode}
@param defaultPort: Optional default port number to be used when SRV
lookup fails and the service name is unknown. This should be the
port number associated with the service name as defined by the IANA
registry.
@type defaultPort: L{int}
"""
self.reactor = reactor
self.service = service
if isinstance(domain, unicode):
domain = domain.encode('idna')
self.domain = nativeString(domain)
self.factory = factory
self.protocol = protocol
self.connectFuncName = connectFuncName
self.connectFuncArgs = connectFuncArgs
self.connectFuncKwArgs = connectFuncKwArgs
self._defaultPort = defaultPort
self.connector = None
self.servers = None
self.orderedServers = None # list of servers already used in this round
示例3: _ebGotServers
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def _ebGotServers(self, failure):
failure.trap(DNSNameError)
# Some DNS servers reply with NXDOMAIN when in fact there are
# just no SRV records for that domain. Act as if we just got an
# empty response and use fallback.
self.servers = []
self.orderedServers = []
示例4: _ebServiceUnknown
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def _ebServiceUnknown(self, failure):
"""
Connect to the default port when the service name is unknown.
If no SRV records were found, the service name will be passed as the
port. If resolving the name fails with
L{error.ServiceNameUnknownError}, a final attempt is done using the
default port.
"""
failure.trap(error.ServiceNameUnknownError)
self.servers = [dns.Record_SRV(0, 0, self._defaultPort, self.domain)]
self.orderedServers = []
self.connect()
示例5: test_SRVPresent
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def test_SRVPresent(self):
"""
Test connectTCP gets called with the address from the SRV record.
"""
payload = dns.Record_SRV(port=6269, target='host.example.org', ttl=60)
client.theResolver.results = [dns.RRHeader(name='example.org',
type=dns.SRV,
cls=dns.IN, ttl=60,
payload=payload)]
self.connector.connect()
self.assertIsNone(self.factory.reason)
self.assertEqual(
self.reactor.tcpClients.pop()[:2], ('host.example.org', 6269))
示例6: test_SRVNoService
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def test_SRVNoService(self):
"""
Test that connecting fails when no service is present.
"""
payload = dns.Record_SRV(port=5269, target=b'.', ttl=60)
client.theResolver.results = [dns.RRHeader(name='example.org',
type=dns.SRV,
cls=dns.IN, ttl=60,
payload=payload)]
self.connector.connect()
self.assertIsNotNone(self.factory.reason)
self.factory.reason.trap(DNSLookupError)
self.assertEqual(self.reactor.tcpClients, [])
示例7: test_lookupService
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def test_lookupService(self):
"""
See L{test_lookupAddress}
"""
d = client.lookupService(self.hostname)
d.addCallback(self.checkResult, dns.SRV)
return d
示例8: lookupServerViaSRV
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def lookupServerViaSRV(domain, service="_ischedules"):
_initResolver()
lookup = "{}._tcp.{}".format(service, domain,)
log.debug("DNS SRV: lookup: {l}", l=lookup)
try:
answers = (yield DebugResolver.lookupService(lookup))[0]
except (DomainError, AuthoritativeDomainError), e:
log.debug("DNS SRV: lookup failed: {exc}", exc=e)
returnValue(None)
示例9: pickServer
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def pickServer(self):
assert self.servers is not None
assert self.orderedServers is not None
if not self.servers and not self.orderedServers:
# no SRV record, fall back..
return self.domain, self.service
if not self.servers and self.orderedServers:
# start new round
self.servers = self.orderedServers
self.orderedServers = []
assert self.servers
self.servers.sort(self._serverCmp)
minPriority=self.servers[0][0]
weightIndex = zip(xrange(len(self.servers)), [x[1] for x in self.servers
if x[0]==minPriority])
weightSum = reduce(lambda x, y: (None, x[1]+y[1]), weightIndex, (None, 0))[1]
rand = random.randint(0, weightSum)
for index, weight in weightIndex:
weightSum -= weight
if weightSum <= 0:
chosen = self.servers[index]
del self.servers[index]
self.orderedServers.append(chosen)
p, w, host, port = chosen
return host, port
raise RuntimeError, 'Impossible %s pickServer result.' % self.__class__.__name__
示例10: lookupService
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def lookupService(self, name, timeout = None):
"""
@see: twisted.names.client.lookupService
"""
return self._lookup(name, dns.IN, dns.SRV, timeout)
示例11: test_SRVPresent
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def test_SRVPresent(self):
"""
Test connectTCP gets called with the address from the SRV record.
"""
payload = dns.Record_SRV(port=6269, target='host.example.org', ttl=60)
client.theResolver.results = [dns.RRHeader(name='example.org',
type=dns.SRV,
cls=dns.IN, ttl=60,
payload=payload)]
self.connector.connect()
self.assertIdentical(None, self.factory.reason)
self.assertEquals(
self.reactor.tcpClients.pop()[:2], ('host.example.org', 6269))
示例12: test_SRVNoService
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def test_SRVNoService(self):
"""
Test that connecting fails when no service is present.
"""
payload = dns.Record_SRV(port=5269, target='.', ttl=60)
client.theResolver.results = [dns.RRHeader(name='example.org',
type=dns.SRV,
cls=dns.IN, ttl=60,
payload=payload)]
self.connector.connect()
self.assertNotIdentical(None, self.factory.reason)
self.factory.reason.trap(DNSLookupError)
self.assertEquals(self.reactor.tcpClients, [])
示例13: lookupService
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def lookupService(self, name, timeout = None):
return self._lookup(name, dns.IN, dns.SRV, timeout)
示例14: pickServer
# 需要导入模块: from twisted.names import dns [as 别名]
# 或者: from twisted.names.dns import SRV [as 别名]
def pickServer(self):
"""
Pick the next server.
This selects the next server from the list of SRV records according
to their priority and weight values, as set out by the default
algorithm specified in RFC 2782.
At the beginning of a round, L{servers} is populated with
L{orderedServers}, and the latter is made empty. L{servers}
is the list of candidates, and L{orderedServers} is the list of servers
that have already been tried.
First, all records are ordered by priority and weight in ascending
order. Then for each priority level, a running sum is calculated
over the sorted list of records for that priority. Then a random value
between 0 and the final sum is compared to each record in order. The
first record that is greater than or equal to that random value is
chosen and removed from the list of candidates for this round.
@return: A tuple of target hostname and port from the chosen DNS SRV
record.
@rtype: L{tuple} of native L{str} and L{int}
"""
assert self.servers is not None
assert self.orderedServers is not None
if not self.servers and not self.orderedServers:
# no SRV record, fall back..
return self.domain, self.service
if not self.servers and self.orderedServers:
# start new round
self.servers = self.orderedServers
self.orderedServers = []
assert self.servers
self.servers.sort(key=lambda record: (record.priority, record.weight))
minPriority = self.servers[0].priority
index = 0
weightSum = 0
weightIndex = []
for x in self.servers:
if x.priority == minPriority:
weightSum += x.weight
weightIndex.append((index, weightSum))
index += 1
rand = random.randint(0, weightSum)
for index, weight in weightIndex:
if weight >= rand:
chosen = self.servers[index]
del self.servers[index]
self.orderedServers.append(chosen)
return str(chosen.target), chosen.port
raise RuntimeError(
'Impossible %s pickServer result.' % (self.__class__.__name__,))