Sharing data between servers and client

I changed the Twisted program client server on this site a bit, which provided a program that could act as a server and client (http://stackoverflow.com/questions/3275004/how-to-write-a- twisted server-what- this-and-a-client). I can connect the client server to an external client on the one hand and to an external server on the other. I want to transfer data from an external client to an external server through a server-client program. The problem I am facing is getting the string received in the ServerProtocol class (in the server-client program) into the ClientProtocol class (in the server-client program). I tried several ways to do this, including trying to use the factory link, as you can see from def init , but I can't get it to work. (for now, I'm just sending literals back and forth to an external server and client) Here is the server-client code:

from twisted.internet import protocol, reactor from twisted.protocols import basic class ServerProtocol(basic.LineReceiver): def lineReceived(self, line): print "line recveived on server-client",line self.sendLine("Back at you from server-client") factory = protocol.ClientFactory() factory.protocol = ClientProtocol reactor.connectTCP('localhost', 1234, factory) class ClientProtocol(basic.LineReceiver): def __init__(self, factory): self.factory = factory def connectionMade(self): self.sendLine("Hello from server-client!") #self.transport.loseConnection() def lineReceived(self, line): print "line recveived on server-client1.py",line #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() 

I should mention that I can connect to the server-client program with an external server and an external client on ports 4321 and 1234, respectively, and they just responded. In addition, I have not shown many attempts to use the self.factory link. Any advice or suggestions would be greatly appreciated.

+4
source share
1 answer

This question is very similar to the popular Twisted FAQ:

How to output one result of the output connection to another?

It doesn’t matter much that the FAQ section deals with many client connections to one server, unlike your question about one incoming client connection and one outgoing client connection. The way you exchange data between different connections is the same.

An important element of this FAQ element is that basically everything you want to do is associated with a method call of some type, and method calls in Twisted are the same as method calls in any other Python program. All you need is a link to the desired object to call the method. 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() 

Please note how:

  • I got rid of the __init__ method on ClientProtocol . ClientFactory calls its protocol with no arguments. __init__ , which requires an argument, will raise a TypeError . In addition, ClientFactory already sets itself as a factory attribute of the generated protocols.
  • I gave ClientProtocol reference to the ServerProtocol instance by setting the ServerProtocol instance as the originator attribute on the factory client. Because the ClientProtocol instance has a reference to the ClientFactory instance, this means that it has a reference to the ServerProtocol instance.
  • I have added the forwardLine method that ClientProtocol can use to directly ServerProtocol to execute any of your application logic after establishing a ClientProtocol connection. Note that because of the previous point, ClientProtocol does not cause problems with calling this method.
+5
source

Source: https://habr.com/ru/post/1415011/


All Articles