from twisted.internet import reactor, protocol, defer
from twisted.protocols import basic
class Client(basic.LineReceiver):
# Internal
def lineReceived(self, data):
self.d.callback(data)
def command(self, cmd):
self.sendLine(cmd)
self.d = defer.Deferred()
return self.d
# public API
def random(self):
def gotRandom(number):
return int(number)
return self.command("random?").addCallback(gotRandom)
def classified(self):
return self.command("classified?")
# User code, this is actually the main()
@defer.inlineCallbacks
def connectionMade(self):
print (yield self.random(None))
print (yield self.classified(None))
reactor.stop()
factory = protocol.ClientFactory()
factory.protocol = Client
reactor.connectTCP("localhost", 6789, factory)
reactor.run()