Pickle file and send via socket

Oscar Benjamin oscar.benjamin at bristol.ac.uk
Wed Aug 8 23:38:28 CEST 2012

On 8 August 2012 16:07, lipska the kat <lipskathekat at yahoo.co.uk> wrote:

> On 08/08/12 14:50, S.B wrote:
>> On Wednesday, August 8, 2012 3:48:43 PM UTC+3, lipska the kat wrote:
>>> On 06/08/12 14:32, S.B wrote:
> [snip]
>  Thank you so much !
>> The examples are very helpful.
>> What happens if I have a regular text file I want to send via the network.
>> Do I need to read the file and then dump it into the "stargate" file
>> object?
Lipska's example code is a good demonstration of how to use sockets and how
to pickle/unpickle objects in order to send them as bytes over a socket.

However, I don't think you really want to pickle the file object (i.e. you
don't want to use the pickle module). It seems that you want to send the
content of the file, which already is bytes, to the other computer. If
that's the case then you can read the bytes of a file with:

filename = 'myfile.txt'
fileobject = open(filename, 'rb')  # We use 'rb' to get bytes, not unicode
filecontents = fileobject.read()    # Reads the whole content of the file
and returns a bytes object

stargate.write(filecontents)   # Actually send the data

In the server program, you can do:

filecontents_received = stargate.read()

Now you have the bytes object and can write it to a file with

filename_server = 'myserverfile.txt'
fileobject_server = open(filename, 'wb')

Notice the difference between the fileobject variable and the filecontents
variable. The subject of your post suggests that you want to send the
fileobject variable (which doesn't make much sense) but your last message
suggests that you're more interested in transferring the filecontents

This has already been said but if you are planning to have this server
program connected to the internet, don't use the pickle module on the data
that you receive. It would make your server vulnerable to being hacked.

> Well according to the documentation at
> http://docs.python.org/py3k/**tutorial/inputoutput.html#**
> reading-and-writing-files<http://docs.python.org/py3k/tutorial/inputoutput.html#reading-and-writing-files>
> it should be straightforward to read and write pickled files
> Not sure why you want to pickle a text file over the network when you
> could just stream it between ports !
> however ...
> I'm currently getting a Unicode decode error on the first byte in the
> stream when it gets to the other end, no idea why so I guess I have to
> continue searching, read the documentation above and see if you can figure
> it out, that's what I'm doing.

Lipska, are you getting the UnicodeDecodeError during the call to
pickle.load? If stargate is opened with the binary flag there shouldn't be
any decoding during the reading of the socket. pickle expects its input to
be binary format so I guess it should only decode when trying to unpickle a
string (I assume these are simply encoded in utf-8). Try just using
stargate.read to see what data arrives. pickled data is often almost human
readable so you might be able to make an educated guess at what it's trying
to do.

Also, the code you posted looks good, but I have one suggestion. Python has
a nicer way of formatting strings using the str.format method. With this
you can replace

"I'm a " + self.name + " class spaceship and I have " + str(self.engines) +
" engines")


"I'm a {0} class spaceship and I have {1} engines".format(self.name,

The advantages of this method are that
1) You keep the template string together so it's easier to read.
2) You don't need to call str() on any of your arguments.
3) It's also more efficient (in some cases).

You can also use named parameters in the format string:

"I'm a {name} class spaceship and I have {number} engines".format(name=
self.name, number=self.engines)

There is also the older, deprecated (but not about to be removed) method:

"I'm a %s class spaceship and I have %i engines" % (self.name, self.engines)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120808/b41f06df/attachment-0001.html>

More information about the Python-list mailing list