python - How to write a twisted server that is also a client? -


How do I create a twisted server that is also a client? I want to listen to the reactor, while at the same time it can also be used to connect to the server instances that can connect and listen.

call reactor.listen TCP and reactor.connect TCP < / Code>. You can have many different types of connections - server or client - as you want.

For example:

  twisted.internet From import protocol, reactor import basic classes from twisted.protocols SomeServerProtocol (basic.LineReceiver): def lineReceived (self, line) : Host, port = line.split () port = integer (port) factory = protocol.ClientFactory () factory.protocol = SomeClientProtocol reactor.connectTCP (host, port, factory) class SomeClientProtocol (basic.LineReceiver): def connectionMade ): Self.sendLine ("Hello") self.transport.loseConnection () DEF (Main): imported from twisted.python import log logs sys. StartLogging (sys.stdout) factory = protocol.ServerFactory () factory.protocol = SomeServerProtocol reactor.listenTCP (12345, factory) reactor.run () if __name__ == '__main__': main ()  

Comments