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
#6
So a snippet my attempted pickling server code:
 
with open(file_requested, 'rb') as temp:
     f = temp.read()
     f = dumps(f)

     # DOESN'T WORK - CAN'T READ 
     segment = f.read(BUFFER)

     self.connection_socket.send(segment)
     print('Sending file....')

     # Run while segment has binary data
     while segment:

          # DOESN'T WORK - CAN'T READ
          segment = f.read(BUFFER)
          self.connection_socket.send(segment)
A snippet my attempted pickling client code:
f = open('new_' + requested_file, 'wb')
segment = client_socket.recv(BUFFER)

# Keep track of the current size of the file as it's downloaded
current_file_size = len(segment)

# Start creating the file with the current segment
f.write(segment)

     # Run until the file is downloaded
     while current_file_size < file_size:
          segment = client_socket.recv(BUFFER)
          current_file_size += len(segment)
          f.write(segment)
# THEN UNPICKLE FILE
So, I know that I can read the parts of the pickled file as it gives me an error. Which means I have to break the file up some other way. Then, I know that my client needs to read each segment differently (maybe as a byte string) and then somehow I need to unpickle the completed file.

I'm new to Python and network programming, so it's a double whammy. Thanks for any help.

Another thought. Let's assume the following:
  • I have successfully grabbed a file (image, pdf, etc) of 10000 bytes from a directory
  • I then pickled the file (we will call it pickledFile)

Can someone give me basic code to send the pickled file (in chunks of 1024 bytes) over a socket connection and then basic code to receive the pickled file (in chunks of 1024 bytes) and then unpickle it?

Then I can see how it's done and modify my code to work.

i think some of the confusion for me is that I was told it is easier to pickle file and then send it as opposed to other options. Though in my original code I just read the file as binary and then sent it in chucks and had no problem.

Thanks.
Reply


Messages In This Thread
RE: Pickle a file over a socket in 1024 segments - by HybridAK - Aug-08-2017, 04:14 AM

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,513 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