<span style="color: rgb(51, 51, 255);">========================================================</span><br><span style="color: rgb(51, 51, 255);"></span><div style="margin-left: 40px;"><span style="color: rgb(51, 51, 255);">
from twisted.internet import protocol, reactor, defer, interfaces</span><br style="color: rgb(51, 51, 255);"><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">class MyProtocol(protocol.Protocol):
</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> is_big_data = False</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> big_data = ''</span>
<br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> big_data_size = 0</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> </span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> def connectionMade(self):</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> self.big_data = ''</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> self.big_data_size = 0</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> self.is_big_data = False</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> pass</span><br style="color: rgb(51, 51, 255);"><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> def dataReceived(self, data): </span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> if check_head_ok(data):</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> # To receive the big size data</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> if self.is_big_data: </span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> return</span><br style="color: rgb(51, 51, 255);">
<br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> # The big data will overflow the Twisted receive buffer size, so loop to receive data</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
if self.is_big_size_data:</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> self.big_data += data</span><br style="color: rgb(51, 51, 255);"><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> if self.big_data_size == len(big_data):</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> # To save data to database</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"> pass</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> </span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
def connectionLost(self, reason):</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> self.big_data = ''</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
self.big_data_size = 0</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> self.is_big_data = False</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
pass</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> </span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> def check_head_ok(self, data):
</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> if data.startswith('ABC'):</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> values =
data.split(',')</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> self.big_data_size = int(values[1])</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
self.is_big_data = self.big_data_size > 0</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> return True</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">
else:</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);"> return False <br></span></div><span style="color: rgb(51, 51, 255);">================================================================
</span><br><span style="color: rgb(51, 51, 255);"></span><div><div><br><span style="color: rgb(153, 0, 0);">I rewrite my source about MyProtocol. But I can not receive complete data, only a part of data. :( Could you help me?
</span><br></div><br></div><br>