Twisted server-client data sharing

前端 未结 1 1258
温柔的废话
温柔的废话 2020-12-15 14:39

I slightly modified a server-client Twisted program on this site, which provided a program that could act as a server and a client (How to write a twisted server that is als

相关标签:
1条回答
  • 2020-12-15 14:50

    This question is very similar to a popular one from the Twisted FAQ:

    How do I make input on one connection result in output on another?

    It doesn't make any significant difference that the FAQ item is talking about many client connections to one server, as opposed to your question about one incoming client connection and one outgoing client connection. The way you share data between different connections is the same.

    The essential take-away from that FAQ item is that basically anything you want to do involves a method call of some sort, and method calls in Twisted are the same as method calls in any other Python program. All you need is to have a reference to the right object to call the method on. So, for example, adapting your code:

    from twisted.internet import protocol, reactor
    from twisted.protocols import basic
    
    class ServerProtocol(basic.LineReceiver):        
        def lineReceived(self, line):
            self._received = line
            factory = protocol.ClientFactory()
            factory.protocol = ClientProtocol
            factory.originator = self
            reactor.connectTCP('localhost', 1234, factory)
    
        def forwardLine(self, recipient):
            recipient.sendLine(self._received)
    
    class ClientProtocol(basic.LineReceiver):
        def connectionMade(self):
            self.factory.originator.forwardLine(self)
            self.transport.loseConnection()
    
    def main():
        import sys
        from twisted.python import log
    
        log.startLogging(sys.stdout)
        factory = protocol.ServerFactory()
        factory.protocol = ServerProtocol
        reactor.listenTCP(4321, factory)
        reactor.run()
    
    if __name__ == '__main__':
        main()
    

    Notice how:

    • I got rid of the __init__ method on ClientProtocol. ClientFactory calls its protocol with no arguments. An __init__ that requires an argument will result in a TypeError being raised. Additionally, ClientFactory already sets itself as the factory attribute of protocols it creates.
    • I gave ClientProtocol a reference to the ServerProtocol instance by setting the ServerProtocol instance as the originator attribute on the client factory. Since the ClientProtocol instance has a reference to the ClientFactory instance, that means it has a reference to the ServerProtocol instance.
    • I added the forwardLine method which ClientProtocol can use to direct ServerProtocol to do whatever your application logic is, once the ClientProtocol connection is established. Notice that because of the previous point, ClientProtocol has no problem calling this method.
    0 讨论(0)
提交回复
热议问题