Python Forum
Pickle a file over a socket in 1024 segments
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pickle a file over a socket in 1024 segments
#3
(Aug-07-2017, 10:36 AM)DeaD_EyE Wrote: You can use on sender side io.BytesIO, which behaves like a file.
Then just read with the given buffer size of 1024 for example and
if the length is > 0, then send it. Otherwise break out of the while True loop.

import io

# your code
# sock = socket.socket()
# sock.connect(('ip', port))

data_to_send = b'Foo' * 2000 # we must send bytes
print('Data to send:', len(data_to_send))

data = io.BytesIO(data_to_send)
while True:
    chunk = data.read(1024)
    if not chunk:
        break
    sock.send(chunk)
What you do before and after this loop, is your responsibility. Maybe you've a protocol to tell the receiver side how much data you are going to send. Maybe you can use a sentinel to tell the receiver side, when the transfer has been finished. Socket programming is not easy.

Thank you for your response!

I guess I should clarify a little bit. I have already successfully been able to transfer a file across the socket connection by reading the the file in bytes and then transferring in way such as you mentioned above.

However, per the instructions I'm supposed to pickle the file and then send it. I have tried opening a file, pickling it, and then sending it in chucks, but it is not working the same. I can not find a single simple example of what I'm trying to do, though I know it's possible. Any thoughts?
Reply


Messages In This Thread
RE: Pickle a file over a socket in 1024 segments - by HybridAK - Aug-07-2017, 06:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to send an image from server to client using PICKLE module dafdaf 1 3,214 Jun-02-2020, 01:08 PM
Last Post: nuffink
  File transfer with xmodem CRC (1024) checksum Jhonbovi 3 8,515 Nov-08-2018, 09:01 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020