Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pickle file and send via socket

839 views
Skip to first unread message

S.B

unread,
Aug 6, 2012, 9:32:13 AM8/6/12
to
Hello friends

Does anyone know if it's possible to pickle and un-pickle a file across a network socket. i.e:
First host pickles a file object and writes the pickled file object to a client socket.
Second host reads the pickled file object from the server socket and un-pickles it.

Can anyone provide a simple code example of the client and server sides?

Thanks

Nobody

unread,
Aug 6, 2012, 12:11:56 PM8/6/12
to
On Mon, 06 Aug 2012 06:32:13 -0700, S.B wrote:

> Does anyone know if it's possible to pickle and un-pickle a file across
> a network socket. i.e: First host pickles a file object and writes the
> pickled file object to a client socket. Second host reads the pickled
> file object from the server socket and un-pickles it.

Yes, provided that the socket is a stream socket (e.g. TCP, Unix), not a
datagram socket (e.g. UDP).

> Can anyone provide a simple code example of the client and server sides?

Pickling via a socket is no different to pickling via a file. For a
socket, the .makefile() method will return a suitable file object.

Christian Heimes

unread,
Aug 6, 2012, 12:45:58 PM8/6/12
to pytho...@python.org
Am 06.08.2012 15:32, schrieb S.B:
> Does anyone know if it's possible to pickle and un-pickle a file across a network socket. i.e:
> First host pickles a file object and writes the pickled file object to a client socket.
> Second host reads the pickled file object from the server socket and un-pickles it.

Have you read the warning in the first paragraph of the pickle docs?
Pickles are a major security risk unless both hosts are trustworthy and
are either inside a protected network or are connected over a secure line.

http://docs.python.org/library/pickle.html#module-pickle

Warning
The pickle module is not intended to be secure against erroneous or
maliciously constructed data. Never unpickle data received from an
untrusted or unauthenticated source.


Christian Heimes

Message has been deleted

S.B

unread,
Aug 7, 2012, 7:21:14 AM8/7/12
to
Lot's of conflicting answers :-(

Can anyone provide a simple code example?

lipska the kat

unread,
Aug 7, 2012, 9:07:11 AM8/7/12
to
On 07/08/12 12:21, S.B wrote:
>>
>>
>>
>> Can anyone provide a simple code example of the client and server sides?

Working on it

lipska

--
Lipska the Kat: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

lipska the kat

unread,
Aug 8, 2012, 8:48:43 AM8/8/12
to
Hi

Firstly I am a raw beginner at Python and I don't publish this code
as a good example of anything. It works for me on Ubuntu Linux 12.04 and
Python3.2

As usual I welcome constructive comments but I will be working on this
more to help me understand exactly what is going on

http://pastebin.com/iFzK7fuk SpaceTravellers.py
http://pastebin.com/TdqPwMGi NetworkPickler.py
http://pastebin.com/DF5DtYRZ NetworkUnpickler.py

S.B

unread,
Aug 8, 2012, 9:50:40 AM8/8/12
to
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 the kat

unread,
Aug 8, 2012, 11:07:33 AM8/8/12
to
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?

Well according to the documentation at

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.

UTKARSH PANDEY

unread,
Feb 17, 2022, 11:59:10 PM2/17/22
to
Directly read bytes from file and send it over the socket object from client side in while loop until all content from file is read.

Something like this.

Client side
import socket

s = socket.socket()

PORT = 9898

s.connect(("192.168.0.101",PORT))

file = open("turnover.csv","rb")
SendData = file.read(1024)

while SendData:
s.send(SendData)
SendData = file.read(1024)


s.close()

Server side


import socket
s = socket.socket()
PORT =9898
print("Server is listening on port :",PORT,"\n")

s.bind(("192.168.0.101",PORT))

s.listen(10)

file = open("recv.csv","wb")
print("\n Copied file name will be recv.txt at server side\n")

while True:
conn,addr = s.accept()
RecvData = conn.recv(1024)
while RecvData:
file.write(RecvData)
RecvData = conn.recv(1024)

file.close()
print("\n File has been copied successfully \n")

conn.close()
print("\n Server is closing the connection \n")


break

Chris Angelico

unread,
Feb 18, 2022, 2:01:02 PM2/18/22
to
On Sat, 19 Feb 2022 at 05:47, UTKARSH PANDEY <u1305...@gmail.com> wrote:
>
> On Wednesday, August 8, 2012 at 8:37:33 PM UTC+5:30, lipska the kat wrote:
> > ...
> Directly read bytes from file and send it over the socket object from client side in while loop until all content from file is read.
>

Almost ten years. Quite an impressive ping time.

ChrisA
0 new messages