def get_deferred_host_ip(): global _host_ip global _host_ip_callbacks global _host_ip_cachetime if hasattr(reactor, 'ident'): assert reactor.ident == thread.get_ident() if _host_ip is not 'unknown' and _host_ip_cachetime + CACHE_TIME > bttime(): return defer.succeed(_host_ip) if get_route_ip: ip = get_route_ip() if ip: _host_ip = ip _host_ip_cachetime = bttime() return defer.succeed(_host_ip) df = defer.Deferred() if not _host_ip_callbacks: def connect(ip): factory = RecorderFactory() factory.protocol = RecorderProtocol if hasattr(reactor, 'limiter'): reactor.connectTCP(ip, 80, factory, urgent=True) else: reactor.connectTCP(ip, 80, factory) rdf = reactor.resolve("ip.bittorrent.com") rdf.addCallback(connect) rdf.addErrback(lambda e : _got_result(None)) _host_ip_callbacks.append(df) return df
def callRemote(self, method, *args, **kwargs): args = (args, kwargs) query = Query(self.path, self.host, method, self.user, self.password, *args) self.factory.addQuery(query) if not self.factory.started: self.factory.started = True def connect(host): if self.secure: from twisted.internet import ssl reactor.connectSSL(host, self.port or 443, self.factory, ssl.ClientContextFactory(), timeout=60) else: reactor.connectTCP(host, self.port or 80, self.factory, timeout=60) df = reactor.resolve(self.host) df.addCallback(connect) df.addErrback(query.deferred.errback) return query.deferred
def get_deferred_host_ip(): global _host_ip global _host_ip_callbacks global _host_ip_cachetime if hasattr(reactor, 'ident'): assert reactor.ident == thread.get_ident() if _host_ip is not 'unknown' and _host_ip_cachetime + CACHE_TIME > bttime( ): return defer.succeed(_host_ip) if get_route_ip: ip = get_route_ip() if ip: _host_ip = ip _host_ip_cachetime = bttime() return defer.succeed(_host_ip) df = defer.Deferred() if not _host_ip_callbacks: def connect(ip): factory = RecorderFactory() factory.protocol = RecorderProtocol if hasattr(reactor, 'limiter'): reactor.connectTCP(ip, 80, factory, urgent=True) else: reactor.connectTCP(ip, 80, factory) rdf = reactor.resolve("ip.bittorrent.com") rdf.addCallback(connect) rdf.addErrback(lambda e: _got_result(None)) _host_ip_callbacks.append(df) return df