[Twisted-Python] Problem in the example code of Twisted Network Programming Essentials

Mike Meng meng.yan at gmail.com
Thu Aug 3 06:03:35 EDT 2006


Hi all,
     I'm learning Twisted by reading Abe Fettig's "Twisted Networking
Programming Essentials". I tried the example 2-6 (dataforward.py) but the
program hung after printing 'Connected to ...'. I don't know  Twisted enough
to figure out where the problem is. Will anybody tell me why? I paste the
code here:

--------------------------------------------------------------
from twisted.internet import stdio, reactor, protocol
from twisted.protocols import basic
import re

class DataForwardingProtocol(protocol.Protocol):
    def __init__(self):
        self.output = None
        self.normalizeNewlines = False

    def dataReceived(self, data):
        if self.normalizeNewlines:
            data = re.sub(r"(\r\n|\n)", "\r\n", data)
        if self.output:
            self.output.write(data)

class StdioProxyProtocol(DataForwardingProtocol):
    def connectionMade(self):
        inputForwarder = DataForwardingProtocol()
        inputForwarder.output = self.transport
        inputForwarder.normalizeNewlines = True
        stdioWrapper = stdio.StandardIO(inputForwarder)
        self.output = stdioWrapper
        print "Connected to server.  Press ctrl-C to close connection."

class StdioProxyFactory(protocol.ClientFactory):
    protocol = StdioProxyProtocol

    def clientConnectionLost(self, transport, reason):
        reactor.stop()

    def clientConnectionFailed(self, transport, reason):
        print reason.getErrorMessage()
        reactor.stop()

if __name__ == '__main__':
    import sys
    if not len(sys.argv) == 3:
        print "Usage: %s host port" % __file__
        sys.exit(1)

    reactor.connectTCP(sys.argv[1], int(sys.argv[2]), StdioProxyFactory())
    reactor.run()
-----------------------------------------------------------------------

   Thanks in advance.

   Mike
Aug. 3, 2006
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20060803/4f94b707/attachment.htm 


More information about the Twisted-Python mailing list