[Twisted-Python] How to receive a big stream data?

steven wang steven.zdwang at gmail.com
Tue Jun 26 22:02:15 EDT 2007


Dear all,

I'm using the Twisted to write a socket server. I has a problem in the
socket server. I could not receive a big size data when the data size is
bigger then the connection buffersize.

The protocol description:

First, Client send data head to Server.  Head Format:
ABC,123456,XXX,XXX,XXX^
Second, Client send big data to Server, the big data is stream data.
After Server received the big data and then save it to database.

My problem source code :
================================================================================================
class MyProtocol(protocol.Protocol):

        is_big_data = False
        big_data = ''
        big_data_size = 0

    def connectionMade(self):
            self.big_data = ''
            self.big_data_size = 0
            self.is_big_data = False
                pass

    def dataReceived(self, data):

         if check_head_ok(data):
            # To receive the big size data
            if self.is_big_data:
                return

        # The big data will overflow the Twisted receive buffer size, so
loop to receive data
        if is_big_size_data:
                big_data += data

                if big_data_size == len(big_data):
                        # To save data to database
                        ....
                        pass

    def connectionLost(self, reason):
          self.big_data = ''
          self.big_data_size = 0
          self.is_big_data = False
          pass

    def check_head_ok(self, head):
          if data.startswith('ABC'):
                  values = data.split(',')
                  self.big_data_size = int(values[1])
                  self.is_big_data = self.big_data_size > 0
                  return True
          else:
                  return False
================================================================================================

If you know it, Please tell me. Thank you very much! :)


Beat Regards,
Steven. Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20070627/8d3e9bbc/attachment.htm 


More information about the Twisted-Python mailing list