Sending data received in one Twisted factory to a second factory

I am trying to write a simple program using the Twisted framework, and I am struggling with resolution (or even with the image, how to write it). I could not find the relevant documentation for:

The main reactor uses two factories, one user that listens for TCP connections on this port (say, 8000) and the second, to enter this IRC server and channel. When receiving data (simple, single line text) in a factory, listening on 8000, I need to transfer this data to the second factory so that it can be processed accordingly - either send a message with this text to the channel, or a personal message to someone, it is not very important now. I can’t find a way to get data from the first factory and send it to another, for processing (maybe like a normal accepted connection for the second IRC factory?).

If this can be somehow resolved, then I would like to add one or several other plants (for example, Jabber) to send the received data to port 8000 to all of them at once, to transfer it accordingly to the protocols (IRC to channel, Jabber to contact and etc.).

Is there anyone who has encountered a similar problem and is willing to give me advice or even share some lines of code? Any help would be greatly appreciated!

Thanks in advance.

+5
source share
3 answers

- . , . , faq :

?

, , Python . Protocol ; transport.write, . Python ; , .

factory, connectionMade connectionLost, . Python:

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

class MultiEcho(Protocol):
    def connectionMade(self):
        self.factory.echoers.append(self)
    def dataReceived(self, data):
        for echoer in self.factory.echoers:
            echoer.transport.write(data)
    def connectionLost(self, reason):
        self.factory.echoers.remove(self)

class MultiEchoFactory(Factory):
    protocol = MultiEcho
    def __init__(self):
        self.echoers = []

reactor.listenTCP(4321, MultiEchoFactory())
reactor.run()
+6

, - "" , .

, factory factory, , . - factory factory .

, factory , . , , MultiEchoFactory, :

 reactor.listenTCP(4321, MultiEchoFactory(someOtherObject))

MultiEchoFactory init:

class MultiEchoFactory(Factory):
    protocol = MultiEcho
    def __init__(self, otherObjectReference):
        self.echoers = []
        self.otherObject = otherObjectReference

.

, , , "" , . ​​, .

0

, .

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

class QOTD(Protocol):

    def connectionMade(self):
        self.factory.message_siblings("Got a client")
        self.transport.loseConnection()

class MyFactory(Factory):
    protocol = QOTD
    def __init__(self,root,name):
        self.clients = []
        self.root = root
        self.name = name
        #self.root.add_child(name,self)
    def message_siblings(self,message):
        self.root.message_children(self.name,message)
    def message_sibling(self,message):
        self.root.message_child(self.name,message)  
    def get_message(self,name,message):
        #do something here
        print name,message



class Container(object):
    def __init__(self):
        self.servers = {}
    def add_child(self,obj,name):
        self.servers[name] = obj(self,name)
    def message_children(self,name,message):
        for server in self.servers:
            if server != name:
                self.servers[server].get_message(name,message)
    def message_child(self,name,message):
        if name in self.servers.keys():
            self.servers[server].get_message(name,message)

container = Container()
container.add_child(MyFactory,'test')
container.add_child(MyFactory,'test2')
reactor.listenTCP(8007, container.servers['test'])
reactor.listenTCP(8008, container.servers['test2'])
reactor.run()

, .

0

All Articles