Question

I'm using python SocketServer to download/upload multi image files.

Server Code

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
    def handle(self):

        '''
        Here is other codes......
        '''

        #start sending files to client...
        for img_name, img_size in dict_image_sizes.iteritems():
            image_file = open(u"%s\\%s" % (image_folder, img_name), 'rb')

            while True:
                image_chunk = image_file.read(32768)

                if not image_chunk:
                    break

                self.request.sendall(image_chunk)

            image_file.close()

Client Code:

    #start recving...
    for img_name, img_size in dict_image_sizes.iteritems():
        each_image_file = open(u'%s\\%s\\%s' % (env.cache_path, relative_path, img_name), 'wb')

        chunk_size = 4096

        img_size_int = int(int(img_size)/chunk_size)
        img_size_mod = int(math.fmod(int(img_size), chunk_size))

        import time
        image_chunk = ''
        for i in range(img_size_int):
            image_chunk += sock.recv(chunk_size)
            #time.sleep(0.001)

        image_chunk += sock.recv(img_size_mod)
        each_image_file.write(image_chunk)

        each_image_file.close()

The question is, if I unmark

            #time.sleep(0.001)

in Client Code, it will okay. If not, many images may get corrupted(not always, sometimes).

Corrupt JPEG data: premature end of data segment

Is there any way to solve this problem? I tried to add

sock.settimeout(1) 

for client, but its not work.

Problem Solved. We need to check send/recv the exact bytes returned each time. Consult Here: Non-blocking socket in Python?

def recvall(sock, size):
  data = ''
  while len(data) < size:
    d = sock.recv(size - len(data))
    if not d:
      # Connection closed by remote host, do what best for you
      return None
    data += d
  return data

def sendall(sock, data):
  while data:
    sent = sock.send(data)
    data = data[sent:]
Was it helpful?

Solution

There's a max OS send/receive buffer size for socket connections. Without time.sleep, all the requests are send instantally the and fill the buffer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top