[Twisted-Python] Simple gateway server

徐乔 sleeepd at gmail.com
Fri Feb 5 23:30:45 MST 2010


I need to write a gateway server that forwarding data between client and
data server。I use a Protocol instance for incoming connection from client,
and creating another Protocol instance to connect data server when received
data from client:

Exp.1
...
Class ToClientProtocol(Protocol):
""" Protocol for incoming connection from client. """

    def connectionMade(self):

        self.data_server = reactor.connectTCP('localhost', 8888,
DataServerFactory())        # make connection to data server

        self.data_server.transport.write('hello!')         # send a message
to data server, but not work
...

The connetion to data server can be made, BUT the data server received
nothing. self.data_server.transport.write() not work.

It will work if I write like that:
Exp. 2
...
Class ToClientProtocol(Protocol):
""" Protocol for incoming connection from client. """

    def connectionMade(self):

        self.data_server = reactor.connectTCP('localhost', 8888,
DataServerFactory(self))        # make connection to data server

Class DataServer(Protocol):
""" Protocol for connection to data server. """

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

    def connectionMade(self):
        self.client.data_server.transport.wirte('hello!')        # send
message to data server, it's work now!

Class DataServerFactory(self):

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

    def buildProtocol(self, addr):
        return DataServer(self.client)
...

I don't think this is the best way. Why I can't simply wirte like Exp.1?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20100206/e53c9bd4/attachment-0001.html>


More information about the Twisted-Python mailing list