Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!news.wiretrip.org!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'received:209.85.212.46': 0.03; 'received:mail- vw0-f46.google.com': 0.03; 'sys': 0.04; 'elif': 0.05; 'char': 0.07; 'dictionary': 0.07; 'seemed': 0.07; 'stuff,': 0.07; 'python': 0.07; 'file-like': 0.09; 'flush': 0.09; 'roger': 0.09; 'output': 0.12; 'server,': 0.12; 'am,': 0.14; 'wrote:': 0.14; '"rb"': 0.16; '[errno': 0.16; 'port))': 0.16; 'pprint': 0.16; 's.accept()': 0.16; 's.close()': 0.16; 'socket.': 0.16; 'worked,': 0.16; '(most': 0.16; 'thanks,': 0.17; '24,': 0.19; 'tue,': 0.20; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'trying': 0.23; 'foundations': 0.23; 'last):': 0.23; 'objects': 0.24; 'example': 0.24; 'received:209.85.212': 0.25; 'skip:# 10': 0.25; "i'm": 0.26; 'instead': 0.26; 'object': 0.27; 'message- id:@mail.gmail.com': 0.28; 'load': 0.28; 'string': 0.29; 'hi,': 0.29; 'server': 0.29; 'skip:" 30': 0.29; 'host': 0.30; 'probably': 0.30; 'sort': 0.30; "won't": 0.30; 'cc:addr:python.org': 0.31; 'transport': 0.31; 'anyone': 0.31; 'import': 0.32; 'creates': 0.33; 'break': 0.33; 'connection': 0.33; 'uses': 0.34; 'using': 0.34; 'skip:" 10': 0.34; 'change': 0.34; 'file': 0.35; 'print': 0.35; 'digit': 0.35; 'too': 0.36; 'program,': 0.36; 'else': 0.37; 'data': 0.37; 'some': 0.37; 'connected': 0.37; 'received:209.85': 0.37; 'run': 0.37; 'apr': 0.38; 'despite': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'port': 0.38; 'url:org': 0.38; 'skip:s 30': 0.39; 'received:209': 0.39; 'how': 0.39; "it's": 0.40; 'header:Received:5': 0.40; 'played': 0.60; '2011': 0.62; 'unique': 0.63; 'below': 0.63; 'below.': 0.64; '(based': 0.84; '11:53': 0.84; 'alexander': 0.84; 'concatenate': 0.84; 'subject:over': 0.84; 'framing': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=1TzOeCHBGOTP0CBhg5u9CYbnRn51dovBMTAFNeR8tPI=; b=w9+GrysScKA2zvcireV7x5E1MtfC1B0oAHNWB2AXHfEyH5hufmBGKYC2dLHYhaTy4G 7TscV6iS55WzUWA7QrM8/tEdDdMvW6ztNLHeiB0S0GUQrGNWkMcIf58kuqh5bVyHb+rh EZUPg0E2e5pHabKgHUjAZy6Ntk0ck8xtIyEcI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=JO/+DvNPZeSwGgqkJELh5pjUzUNhLOySEMsG6IIApOzG1bNOcN5E2xbYhRVq7mSq2d Bgpu3BGp4dSXYkvEVjde1ZiKjv90gWObr6XMMKLMI3934p1Xg4+LTbPPUO28KIiFumvm /ycTigYaimgGBPab2YgZ1idmN0wfN43Mk7YVY= MIME-Version: 1.0 In-Reply-To: <61890800-f81a-4a1e-8905-a0237407f016@a21g2000prj.googlegroups.com> References: <61890800-f81a-4a1e-8905-a0237407f016@a21g2000prj.googlegroups.com> Date: Tue, 19 Apr 2011 12:30:53 -0700 Subject: Re: Pickling over a socket From: Dan Stromberg To: Roger Alexander Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 117 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303241456 news.xs4all.nl 41102 [::ffff:82.94.164.166]:38553 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3585 On Tue, Apr 19, 2011 at 11:53 AM, Roger Alexander wro= te: > Hi, > > I'm trying to understand how to pickle Python objects over a TCP > socket. > > In the example below (based on code from Foundations of Python Network > Programming), a client creates a dictionary (lines 34-38) and uses > pickle.dump at line 42 to write the pickled object using file handle > make from a socket. The server de-pickles with pickle.load =A0(line 24), > again using a file handle made from a socket. > > When I run the program, the following output is produced: > > =A0 =A0Listening at ('127.0.0.1', 1060) > =A0 =A0Accepted connection from ('127.0.0.1', 49938) > =A0 =A0Traceback (most recent call last): > =A0 =A0File "pickles.py", line 24, in > =A0 =A0 =A0 =A0d =3D pickle.load( s_fh ) > =A0 =A0File "/usr/local/lib/python2.7/pickle.py", line 1378, in load > =A0 =A0 =A0 =A0return Unpickler(file).load() > =A0 =A0File "/usr/local/lib/python2.7/pickle.py", line 857, in load > =A0 =A0 =A0 =A0key =3D read(1) > =A0 =A0File "/usr/local/lib/python2.7/socket.py", line 380, in read > =A0 =A0 =A0 =A0data =3D self._sock.recv(left) > =A0 =A0socket.error: [Errno 107] Transport endpoint is not connected > > I'm at a loss, can anyone provide any guidance? > > Thanks, > > Roger Alexander I played around with it until something worked, and ended up with the below. The most significant change was probably using sc.makefile instead of s.makefile in the server, but I seemed to need some data framing too despite the pickling. It's possible you won't need that if you just flush your file in the client; I don't much pickling experience - in particular, I don't know if you can concatenate pickled objects and load them serially from a file-like object without any (additional) framing. I like to use bufsock for this sort of stuff, but I'm probably unique in that. ^_^ http://stromberg.dnsalias.org/~strombrg/bufsock.html #!/usr/bin/python import time import pickle import socket, sys import pprint s =3D socket.socket(socket.AF_INET, socket.SOCK_STREAM) HOST =3D sys.argv.pop() if len(sys.argv) =3D=3D 3 else '127.0.0.1' PORT =3D 1060 if sys.argv[1:] =3D=3D ['server']: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((HOST, PORT)) s.listen(1) print 'Listening at', s.getsockname() sc, sockname =3D s.accept() print 'Accepted connection from', sockname sf =3D sc.makefile( "rb" ) length_list =3D [] while True: char =3D sf.read(1) if char =3D=3D '\n': break else: length_list.append(int(char)) length =3D 0 for digit in length_list: length =3D length * 10 + digit data =3D sf.read(length) d =3D pickle.loads(data) pprint.pprint(d) sc.shutdown(socket.SHUT_RDWR) sc.close() s.close() elif sys.argv[1:] =3D=3D ['client']: s.connect((HOST, PORT)) # s.shutdown(socket.SHUT_RD) d =3D dict() d[ 'Name' ] =3D 'Jake Thompson.' d[ 'Age' ] =3D 25 d[ 'Location' ] =3D 'Washington, D.C.' sf =3D s.makefile( "wb" ) string =3D pickle.dumps( d, pickle.HIGHEST_PROTOCOL ) sf.write('%d\n' % len(string)) sf.write(string) sf.flush() #time.sleep(10) sf.close() s.shutdown(socket.SHUT_RDWR) # s.close() else: print >>sys.stderr, 'usage: streamer.py server|client [host]'