[Twisted-Python] Client Prompt

Hi everybody, Maybe this is a very stupid question, but I realy couldn't find a way to solve it by myself. Would be enough for my aplication if this example works. 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 ################################################################# from twisted.internet import reactor, protocol class EchoClient(protocol.Protocol): def connectionMade(self): """I don't want to write it here I want to take data from stdin and send o server, but were and how to do it? Too much dificult? """ self.transport.write("hello, world!") 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() ################################################################# Thanks for all -- Daniel Henrique Debonzi Linux user number 166903 Gwyddion Embedded Free-Software Development www.gwyddion.com São Carlos, São Paulo Brazil

On Mon, Feb 16, 2004 at 10:47:47PM -0300, Daniel Henrique Debonzi wrote:
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.

On Mon, Feb 16, 2004 at 10:47:47PM -0300, Daniel Henrique Debonzi wrote:
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.
participants (2)
-
Daniel Henrique Debonzi
-
Sam Jordan