本文整理汇总了Python中twisted.internet.reactor.resolve方法的典型用法代码示例。如果您正苦于以下问题:Python reactor.resolve方法的具体用法?Python reactor.resolve怎么用?Python reactor.resolve使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.reactor
的用法示例。
在下文中一共展示了reactor.resolve方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resolveHost
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def resolveHost(self, host):
address = self.dnsCache.getCachedAddress(host)
if address != None:
log.debug("Host cached: {} {}".format(host, address))
return defer.succeed(address)
else:
log.debug("Host not cached.")
self.customResolver.port = self.urlMonitor.getResolverPort()
try:
log.debug("Resolving with DNSChef")
address = str(self.customResolver.query(host)[0].address)
return defer.succeed(address)
except Exception:
log.debug("Exception occured, falling back to Twisted")
return reactor.resolve(host)
示例2: get
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def get(self):
host = self.config.get('destination', self.hostname)
try:
ip = yield reactor.resolve(host)
except:
ip = None
if ip:
try:
loss, latency = yield icmp.ping(ip, 5)
except:
loss, latency = 100, None
event = [self.createEvent('ok', '%s%% loss to %s' % (loss,host), loss,
prefix="loss")]
if latency:
event.append(self.createEvent('ok', 'Latency to %s' % host, latency,
prefix="latency"))
else:
event = [self.createEvent('critical', 'Unable to resolve %s' % host, 100,
prefix="loss")]
defer.returnValue(event)
示例3: resolveHost
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def resolveHost(self, host):
address = self.dnsCache.getCachedAddress(host)
if address != None:
logging.debug("Host cached.")
return defer.succeed(address)
else:
logging.debug("Host not cached.")
return reactor.resolve(host)
示例4: testChildResolve
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def testChildResolve(self):
# I've seen problems with reactor.run under gtk2reactor. Spawn a
# child which just does reactor.resolve after the reactor has
# started, fail if it does not complete in a timely fashion.
helperPath = os.path.abspath(self.mktemp())
with open(helperPath, 'w') as helperFile:
# Eeueuuggg
reactorName = reactor.__module__
helperFile.write(resolve_helper % {'reactor': reactorName})
env = os.environ.copy()
env['PYTHONPATH'] = os.pathsep.join(sys.path)
helperDeferred = Deferred()
helperProto = ChildResolveProtocol(helperDeferred)
reactor.spawnProcess(helperProto, sys.executable, ("python", "-u", helperPath), env)
def cbFinished(result):
(reason, output, error) = result
# If the output is "done 127.0.0.1\n" we don't really care what
# else happened.
output = b''.join(output)
if _PY3:
expected_output = (b'done 127.0.0.1' +
os.linesep.encode("ascii"))
else:
expected_output = b'done 127.0.0.1\n'
if output != expected_output:
self.fail((
"The child process failed to produce the desired results:\n"
" Reason for termination was: %r\n"
" Output stream was: %r\n"
" Error stream was: %r\n") % (reason.getErrorMessage(), output, b''.join(error)))
helperDeferred.addCallback(cbFinished)
return helperDeferred
示例5: resolveHost
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def resolveHost(self, host):
address = self.dnsCache.getCachedAddress(host)
if address != None:
log.debug("[ClientRequest] Host cached: {} {}".format(host, address))
return defer.succeed(address)
else:
return reactor.resolve(host)
示例6: __init__
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def __init__(self, channel, queued, reactor=reactor):
Request.__init__(self, channel, queued)
self.reactor = reactor
self.urlMonitor = URLMonitor.getInstance()
self.hsts = URLMonitor.getInstance().hsts
self.cookieCleaner = CookieCleaner.getInstance()
self.dnsCache = DnsCache.getInstance()
#self.uniqueId = random.randint(0, 10000)
#Use are own DNS server instead of reactor.resolve()
self.customResolver = dns.resolver.Resolver()
self.customResolver.nameservers = ['127.0.0.1']
示例7: createClient
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def createClient(self):
"""Create a UDP connection to Riemann"""
server = self.config.get('server', '127.0.0.1')
port = self.config.get('port', 5555)
def connect(ip):
self.protocol = riemann.RiemannUDP(ip, port)
self.endpoint = reactor.listenUDP(0, self.protocol)
d = reactor.resolve(server)
d.addCallback(connect)
return d
示例8: get_local_ip
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def get_local_ip():
"""
Returns a deferred which will be called with a
2-uple (lan_flag, ip_address) :
- lan_flag:
- True if it's a local network (RFC1918)
- False if it's a WAN address
- ip_address is the actual ip address
@return: A deferred called with the above defined tuple
@rtype: L{twisted.internet.defer.Deferred}
"""
# first we try a connected udp socket, then via multicast
logging.debug("Resolving dns to get udp ip")
try:
ipaddr = yield reactor.resolve('A.ROOT-SERVERS.NET')
except:
pass
else:
udpprot = DatagramProtocol()
port = reactor.listenUDP(0, udpprot)
udpprot.transport.connect(ipaddr, 7)
localip = udpprot.transport.getHost().host
port.stopListening()
if is_bogus_ip(localip):
raise RuntimeError, "Invalid IP address returned"
else:
defer.returnValue((is_rfc1918_ip(localip), localip))
logging.debug("Multicast ping to retrieve local IP")
ipaddr = yield _discover_multicast()
defer.returnValue((is_rfc1918_ip(ipaddr), ipaddr))
示例9: testChildResolve
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def testChildResolve(self):
# I've seen problems with reactor.run under gtk2reactor. Spawn a
# child which just does reactor.resolve after the reactor has
# started, fail if it does not complete in a timely fashion.
helperPath = os.path.abspath(self.mktemp())
helperFile = open(helperPath, 'w')
# Eeueuuggg
reactorName = reactor.__module__
helperFile.write(resolve_helper % {'reactor': reactorName})
helperFile.close()
env = os.environ.copy()
env['PYTHONPATH'] = os.pathsep.join(sys.path)
helperDeferred = Deferred()
helperProto = ChildResolveProtocol(helperDeferred)
reactor.spawnProcess(helperProto, sys.executable, ("python", "-u", helperPath), env)
def cbFinished((reason, output, error)):
# If the output is "done 127.0.0.1\n" we don't really care what
# else happened.
output = ''.join(output)
if output != 'done 127.0.0.1\n':
self.fail((
"The child process failed to produce the desired results:\n"
" Reason for termination was: %r\n"
" Output stream was: %r\n"
" Error stream was: %r\n") % (reason.getErrorMessage(), output, ''.join(error)))
helperDeferred.addCallback(cbFinished)
return helperDeferred
示例10: resolveAddress
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def resolveAddress(self):
host, port = self.addr
if isIPAddress(host):
return self.addr
else:
from twisted.internet import reactor
return reactor.resolve(host).addCallback(self._filterRealAddress)
示例11: testChildResolve
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def testChildResolve(self):
# I've seen problems with reactor.run under gtk2reactor. Spawn a
# child which just does reactor.resolve after the reactor has
# started, fail if it does not complete in a timely fashion.
helperPath = os.path.abspath(self.mktemp())
helperFile = open(helperPath, 'w')
# Eeueuuggg
reactorName = reactor.__module__
helperFile.write(resolve_helper % {'reactor': reactorName})
helperFile.close()
env = os.environ.copy()
env['PYTHONPATH'] = os.pathsep.join(sys.path)
helperDeferred = Deferred()
helperProto = ChildResolveProtocol(helperDeferred)
reactor.spawnProcess(helperProto, sys.executable, ("python", "-u", helperPath), env)
def cbFinished((reason, output, error)):
# If the output is "done 127.0.0.1\n" we don't really care what
# else happened.
output = ''.join(output)
if output != 'done 127.0.0.1\n':
self.fail((
"The child process failed to produce the desired results:\n"
" Reason for termination was: %r\n"
" Output stream was: %r\n"
" Error stream was: %r\n") % (reason.getErrorMessage(), output, ''.join(error)))
helperDeferred.addCallback(cbFinished)
return helperDeferred
示例12: joinNetwork
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def joinNetwork(self):
"""加入DHT网络"""
for address in BOOTSTRAP_NODES:
self.resolve(address[0], address[1])
reactor.callLater(KRPC_TIMEOUT, self.joinFailHandle)
示例13: resolve
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import resolve [as 别名]
def resolve(self, host, port):
"""解析域名"""
def callback(ip, port):
"""解析成功后, 开始发送find_node"""
self.findNode((ip, port))
def errback(failure, host, port):
"""解析失败, 再继续解析, 直到成功为止"""
self.resolve(host, port)
d = reactor.resolve(host)
d.addCallback(callback, port)
d.addErrback(errback, host, port)