[Twisted-Python] two twisted clients, how can I trigger the other one's callback in one's callback?

Arnar Birgisson arnarbi at gmail.com
Wed Jun 20 07:52:06 EDT 2007

On 6/20/07, slowtech chen <slowtech.chen at yahoo.com> wrote:
> I have 2 clients, one gets a message from a server, once I get
> the message, I want to send it with the other client to another
> server. how can i do this?  can I add some customized events
> and notice the other client? Thanks.

Are they both running in the same process?

This is my first non-question post on this list, so please forgive me
if I'm giving bad advice - hopefully someone will correct me if I am.

If they're both in the same process you can just make calls between
them normally.

from twisted.internet.protocol import Protocol
from twisted.internet import reactor, protocol

class YourProtocol(Protocol):

    def connectionMade(self):
        print "connected"

    def connectionLost(self, reason):
        print "disconnected"

    def dataReceived(self, data):
        # repeat the data to all other connections
        print "relaying data"
        for conn in self.factory.connections:
            if conn != self:

class YourClientFactory(protocol.ClientFactory):
    protocol = YourProtocol

    def __init__(self):
        self.connections = []

if __name__ == '__main__':

    f = YourClientFactory()
    reactor.connectTCP("hostname1", 10001, f)
    reactor.connectTCP("hostname2", 10002, f)

More information about the Twisted-Python mailing list