[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):
        self.realProto.transport.write(data)

class EchoClient(protocol.Protocol):

     def connectionMade(self):
        stdio.StandardIO(InputHandler(self))

     def dataReceived(self, data):
         print data

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

class EchoFactory(protocol.ClientFactory):
     protocol = EchoClient

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

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

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

if __name__ == '__main__':
     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