Feels Code Man
Feels Code Man

Reputation: 53

Python Twisted - Server communication

I'm having a bizarre issue. Basically, the problem I have right now is dealing with two different LineReceiver servers that are connected to each other. Essentially, if I were to input something into server A, then I want some output to appear in server B. And I would like to do this vice versa. I am running two servers on two different source files (also running them on different processes via & shellscript) ServerA.py and ServerB.py where the ports are (12650 and 12651) respectively. I am also connecting to each server using telnet.

from twisted.internet import protocol, reactor
from twisted.protocols.basic import LineReceiver

class ServerA(LineReceiver);
   def connectionMade(self):
      self.transport.write("Is Server A\n")
   def dataReceived(self, data):
      self.sendLine(data)
   def lineReceived(self, line):
      self.transport.write(line)

def main():
   client = protocol.ClientFactory()
   client.protocol = ServerA
   reactor.connectTCP("localhost", 12650, client)

   server = protocol.ServerFactory()
   server.protocol = ServerA
   reactor.listenTCP(12651, server)

   reactor.run()

if __name__ == '__main__':
   main()

My issue is the use of sendLine. When I try to do a sendLine call from serverA with some arbitrary string, serverA ends up spitting out the exact string instead of sending it down the connection which was done in main(). Exactly why is this happening? I've been looking around and tried each solution I came across and I can't seem to get it to work properly. The bizarre thing is my friend is essentially doing the same thing and he gets some working results but this is the simplest program I could think of to try to figure out the cause for this strange phenomenon.

In any case, the gist is, I'm expecting to get the input I put into serverA to appear in serverB.

Note: Server A and Server B have the exact same source code save for the class names and ports.

Upvotes: 1

Views: 685

Answers (1)

Glyph
Glyph

Reputation: 31910

You have overridden dataReceived. That means that lineReceived will never be called, because it is LineReceiver's dataReceived implementation that eventually calls lineReceived, and you're never calling up to it.

You should only need to override lineReceived and then things should work as you expect.

Upvotes: 1

Related Questions