def run_proxy(args=None): service = MultiService() options = get_options(args) open_log(os.path.join(options['gamedir'], 'proxy.log')) pidfile = os.path.join(options['gamedir'], 'proxy.pid') check_pid(pidfile) config.read(options.config_paths()) port = config.getint('Proxy', 'AMP port') factory = ReconnectingClientFactory() factory.maxDelay = 1 factory.protocol = AmpClientProtocol client = internet.TCPClient('127.0.0.1', port, factory) service.addService(client) ports_str = config.get('Proxy', 'telnet ports') for portVal in ports_str.split(','): try: port = int(portVal) except ValueError: continue factory = ServerFactory() factory.protocol = ProxyTelnetSession child = internet.TCPServer(port, factory) child.setName("ProxyTelnet%d" % port) service.addService(child) service.startService() reactor.run() os.remove(pidfile)
def test_stopTryingDoesNotReconnect(self): """ Calling stopTrying on a L{ReconnectingClientFactory} doesn't attempt a retry on any active connector. """ class FactoryAwareFakeConnector(FakeConnector): attemptedRetry = False def stopConnecting(self): """ Behave as though an ongoing connection attempt has now failed, and notify the factory of this. """ f.clientConnectionFailed(self, None) def connect(self): """ Record an attempt to reconnect, since this is what we are trying to avoid. """ self.attemptedRetry = True f = ReconnectingClientFactory() f.clock = Clock() # simulate an active connection - stopConnecting on this connector should # be triggered when we call stopTrying f.connector = FactoryAwareFakeConnector() f.stopTrying() # make sure we never attempted to retry self.assertFalse(f.connector.attemptedRetry) self.assertFalse(f.clock.getDelayedCalls())
def test_serializeUnused(self): """ A L{ReconnectingClientFactory} which hasn't been used for anything can be pickled and unpickled and end up with the same state. """ original = ReconnectingClientFactory() reconstituted = pickle.loads(pickle.dumps(original)) self.assertEqual(original.__dict__, reconstituted.__dict__)
def test_serializeWithClock(self): """ The clock attribute of L{ReconnectingClientFactory} is not serialized, and the restored value sets it to the default value, the reactor. """ clock = Clock() original = ReconnectingClientFactory() original.clock = clock reconstituted = pickle.loads(pickle.dumps(original)) self.assertIsNone(reconstituted.clock)
def test_parametrizedClock(self): """ The clock used by L{ReconnectingClientFactory} can be parametrized, so that one can cleanly test reconnections. """ clock = Clock() factory = ReconnectingClientFactory() factory.clock = clock factory.clientConnectionLost(FakeConnector(), None) self.assertEqual(len(clock.calls), 1)
def __init__(self, spkn_msg_obj_creator, queue_obj, exp_conn, is_expecting_data=False): ReconnectingClientFactory().__init__() self.message_object = spkn_msg_obj_creator self.maxRetries = 1 self.exp_conn = exp_conn self.total_connected = 0 self.total_verified = 0 self.total_rejected = 0 self.total_conn_fail = 0 self.q_object = queue_obj self.is_expecting_data = is_expecting_data
def test_deserializationResetsParameters(self): """ A L{ReconnectingClientFactory} which is unpickled does not have an L{IConnector} and has its reconnecting timing parameters reset to their initial values. """ factory = ReconnectingClientFactory() factory.clientConnectionFailed(FakeConnector(), None) self.addCleanup(factory.stopTrying) serialized = pickle.dumps(factory) unserialized = pickle.loads(serialized) self.assertIsNone(unserialized.connector) self.assertIsNone(unserialized._callID) self.assertEqual(unserialized.retries, 0) self.assertEqual(unserialized.delay, factory.initialDelay) self.assertTrue(unserialized.continueTrying)
def testStopTrying(self): f = Factory() f.protocol = In f.connections = 0 f.allMessages = [] f.goal = 2 f.d = defer.Deferred() c = ReconnectingClientFactory() c.initialDelay = c.delay = 0.2 c.protocol = Out c.howManyTimes = 2 port = self.port = reactor.listenTCP(0, f) PORT = port.getHost().port reactor.connectTCP('127.0.0.1', PORT, c) f.d.addCallback(self._testStopTrying_1, f, c) return f.d
def test_stopTryingWhenConnected(self): """ If a L{ReconnectingClientFactory} has C{stopTrying} called while it is connected, it does not subsequently attempt to reconnect if the connection is later lost. """ class NoConnectConnector(object): def stopConnecting(self): raise RuntimeError("Shouldn't be called, we're connected.") def connect(self): raise RuntimeError("Shouldn't be reconnecting.") c = ReconnectingClientFactory() c.protocol = Protocol # Let's pretend we've connected: c.buildProtocol(None) # Now we stop trying, then disconnect: c.stopTrying() c.clientConnectionLost(NoConnectConnector(), None) self.assertFalse(c.continueTrying)
class MLStripper(HTMLParser): def __init__(self): self.reset() self.fed = [] def handle_data(self, d): self.fed.append(d) def get_data(self): return ''.join(self.fed) def strip(html): stripper = MLStripper() stripper.feed(html) s = stripper.get_data() return s.strip().replace("\n", " ") if __name__ == "__main__": from twisted.internet import reactor from twisted.internet.task import LoopingCall factory = ReconnectingClientFactory() factory.protocol = IRCClient reactor.connectTCP("irc.freenode.net", 6667, factory) poller = FeedPoller(source) task = LoopingCall(poller.check) task.start(10) reactor.run()
def run(config): factory = ReconnectingClientFactory() factory.protocol = RelayToIRC factory.config = config reactor.connectTCP(config["irc"]["host"], config["irc"]["port"], factory) reactor.run()