[Twisted-Python] Client Prompt

Sam Jordan sam at twistedmatrix.com
Tue Feb 17 10:50:12 EST 2004

On Mon, Feb 16, 2004 at 10:47:47PM -0300, Daniel Henrique Debonzi wrote:
> So, I want to have a client that read a string from the stdin and then 
> send it to the server. Wich changes should I do in this code to this 
> client work like a discrebed

You most probably want to use twisted.internet.stdio.StandardIO.
I have made a few simple modifications to your code to demonstrate
how this works -- note that this is just to demonstrate functionality
and makes a few assumptions (such as your terminal line-buffering input).

from twisted.internet import reactor, protocol, stdio

class InputHandler(protocol.Protocol):

    def __init__(self, realProto):
        self.realProto = realProto

    def dataReceived(self, data):

class EchoClient(protocol.Protocol):

     def connectionMade(self):

     def dataReceived(self, data):
         print data

     def connectionLost(self, reason):
         print "connection lost"
         from twisted.internet import reactor

class EchoFactory(protocol.ClientFactory):
     protocol = EchoClient

     def clientConnectionFailed(self, connector, reason):
         print "Connection failed - goodbye!"

     def clientConnectionLost(self, connector, reason):
         print "Connection lost - goodbye!"

def main():
     f = EchoFactory()
     reactor.connectTCP("localhost", 5555, f)

if __name__ == '__main__':


You may also want to take a look at doc/examples/cursesclient.py.

Hope this helps,

Sam Jordan.

More information about the Twisted-Python mailing list