1

I need to break a large object (say a File Object) into 4kb sized chunks to send over a socket in my client-server application, but I can't fathom how. I'm using python.

lightandlight
  • 1,345
  • 3
  • 10
  • 24
  • @lightandlight Do you need to send the actual object, or just the data in the file? – Mac O'Brien May 28 '14 at 04:11
  • @ceann The data. My end-goal for this is to stream an mp3, so I would need to send each frame. But right now I just need to figure out how to split large messages – lightandlight May 28 '14 at 04:13
  • It's basically the same either way except if it is an object you'll need to pickle it or some other form of serialization first in place of a file. – woot May 28 '14 at 04:15
  • @woot yeah I'm reading through the link you added and it looks promising – lightandlight May 28 '14 at 04:16

1 Answers1

0
        with open(path-to-file, "rb") as fi:
            buf = fi.read(4096)
            while (buf):
               send(buf)
               buf = fi.read(4096)

Where send() is a method that will actually send the 4K chunk of data

Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40