Reputation: 55
I am trying to send two messages using Twisted (see below) one after the other, but the second one isn't getting received.
The server prints out the messages it receieves:
New connections (1) made... Total : 1
{'ID': 1}
{'ID': 0}
{'ID': 0}
Client code is:
class TestClient(protocol.Protocol):
def SendKeepAliveMsg(self):
lst = {"ID" : 0}
self.transport.write(pickle.dumps(lst))
def SendMsg_StartHandshake(self, unused):
lst = {"ID" : 1}
self.transport.write(pickle.dumps(lst))
def SendMsg_CompleteHandshake(self, unused):
lst = {"ID" : 2}
self.transport.write(pickle.dumps(lst))
def connectionMade(self):
d = Deferred()
d.addCallback(self.SendMsg_StartHandshake)
d.addCallback(self.SendMsg_CompleteHandshake)
d.callback(None)
lc = LoopingCall(self.SendKeepAliveMsg)
lc.start(3)
Am I doing the writes in the correct way? I do want them to be two unique messages.
Upvotes: 3
Views: 159
Reputation: 48335
First, don't ever use pickle as a wire protocol. The pickle module allows arbitrary code execution during unpickling. If you load pickle data from the network, you are building a remote code execution vulnerability right into your software. Never do this. It doesn't matter if you trust the peer or if it's just for a small internal project or if there's a firewall or you have some kind of authentication. It will cause a problem eventually. There's no good reason to trade away all of your security for what pickle does - there are lots of options that provide similar functionality without the security problems.
With that out of the way, you writes are probably actually all fine and all going through. The problem is that you don't have a framing protocol around your messages. All of your writes probably get combined and the reader gets them all in a single read. When it interprets the data it has read, it parses the first message and drops the rest.
Use a better protocol like HTTP, AMP, or any of a dozen others, and the problem will resolve itself.
Upvotes: 1