What Data is Available With a Pickled Object Over a Socket?

M

milan_sanremo

I've read the library entry for pickle a couple of times, and I'm
still not
sure what data is maintained when an item is pickled and sent over a
socket.

Two importable modules:

class envelope():
def __init__(self, rec, d):
self.recipient = rec
self.listData = d

class mailbag():
def __init__(self):
self.dicEnvelope = []

def addEnvelope(self, uuid, envelope):
self.dicEnvelope[uuid] = envelope


Client
---------
import envelope, mailbag, cPickle, socket

remoteHost = "999.666.888.777
mb = mailbag()
env1 = envelope('John', ('a', 'b', 'c'))
env2 = envelope('Mary', ('d', 'e', f'))

mb.addEnvelope(1, env1)
mb.addEvenlop(2, env2)

pickledMailbag = cPickle.dump(mb, HIGHEST_PROTOCOL)

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(remoteHost)
sock.send(pickledMailbag)

Server
---------
import envelope, mailbag, cPickle, socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(host, port)
sock.listen(5)
conn, addr = sock.accept()
while 1:
pickledData = conn.recv(1024)
if not pickledData break

mb = cPickle.load(pickledData)

At this point, does mb contain a dictionary with the two envelopes
with
their data?
 
G

Gabriel Genellina

En Thu, 18 Oct 2007 19:21:15 -0300, milan_sanremo
I've read the library entry for pickle a couple of times, and I'm
still not
sure what data is maintained when an item is pickled and sent over a
socket.

For most "normal" objects, all their instance attributes are stored.
pickledMailbag = cPickle.dump(mb, HIGHEST_PROTOCOL)

Please copy&paste actual code when posting, don't retype it...
The above line should read cPickle.dumps, I presume.
while 1:
pickledData = conn.recv(1024)
if not pickledData break

Instead of reinventing the wheel again, I'd try to use the existing server
classes in the standard library, like SimpleHTTPServer by example. Among
other things, you are not accumulating pickledData.
mb = cPickle.load(pickledData)

At this point, does mb contain a dictionary with the two envelopes
with
their data?

Asuming you managed to get pickledData right, and you use cPickle.loads:
yes, mb should contain the same things as the original.
 
H

Hendrik van Rooyen

pickledMailbag = cPickle.dump(mb, HIGHEST_PROTOCOL)

If you are going to send it over a socket - is it not better to use
dumps instead of dump?

- Hendrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top