Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'sys': 0.04; 'dictionary': 0.07; 'python': 0.07; 'roger': 0.09; 'output': 0.12; 'received:74.125.82.174': 0.12; 'received:mail- wy0-f174.google.com': 0.12; 'am,': 0.14; 'wrote:': 0.14; '"rb"': 0.16; '[errno': 0.16; 'opposite': 0.16; 'port))': 0.16; 's.accept()': 0.16; 'skip:\xc2 30': 0.16; 'socket.': 0.16; '\xc2\xa0if': 0.16; '(most': 0.16; 'thanks,': 0.17; 'url:blog': 0.18; '24,': 0.19; 'tue,': 0.20; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'cheers,': 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; "i'm": 0.26; 'chris': 0.27; 'object': 0.27; 'tried': 0.27; 'message-id:@mail.gmail.com': 0.28; 'load': 0.28; 'hi,': 0.29; 'server': 0.29; 'skip:" 30': 0.29; 'cc:addr:python.org': 0.31; 'shut': 0.31; 'skip:\xc2 20': 0.31; 'transport': 0.31; 'url:library': 0.31; 'anyone': 0.31; 'done': 0.32; 'url:docs': 0.33; 'creates': 0.33; 'programming,': 0.33; 'connection': 0.33; 'uses': 0.34; 'using': 0.34; 'skip:" 10': 0.34; 'file': 0.35; 'platform,': 0.35; 'program,': 0.36; 'else': 0.37; 'connected': 0.37; 'url:python': 0.37; 'run': 0.37; 'apr': 0.38; 'removing': 0.38; 'received:google.com': 0.38; 'skip:" 20': 0.38; 'url:org': 0.38; '8bit%:6': 0.39; 'how': 0.39; 'header:Received:5': 0.40; '8bit%:11': 0.60; '8bit%:14': 0.60; '2011': 0.62; 'below': 0.63; 'connection.': 0.77; 'skip:\xc2 10': 0.83; '(based': 0.84; '11:53': 0.84; 'alexander': 0.84; 'subject:over': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=7aYnga2S7DJa0MdFXvO4r3FSYFajk9Bio9nHGADs+vY=; b=AgwjSa2eUImlAzz0m+23aLUqgQ6pTRoeeNioNORauPwwmPISwI6zQ5JMtb1diXV6Pj BgEd63rSFmfMC/cjpiBTRdVAkevy7b+2cOycLDaWXatFMa+tmcwDMuwTydeJOZhFKs9f 4eeAJqNPrQA9uAFCb20iDvqffLuXeppUZfrvA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=Iq9YaEryHaVVDavyLGWxCkHkYYJNTi2Ko7Y7gchtuyKKb7fA64hAb4ayY2geNYkNqh cUW+OqHNYKvnMFgQQ5FxvCrjfZO460T5AyjxcSzzk0YmB9dhcs2X++JcmM6G66XI5t2G P9kensac+xOd+fMlXPmCOokXxTEMHEAEgXZUI= MIME-Version: 1.0 Sender: chris@rebertia.com 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:21:57 -0700 X-Google-Sender-Auth: 044bA9JDq053pZycznsH9bo9Ae4 Subject: Re: Pickling over a socket From: Chris Rebert To: Roger Alexander Content-Type: text/plain; charset=UTF-8 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: 91 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303240920 news.xs4all.nl 81476 [::ffff:82.94.164.166]:37753 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3583 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 =C2=A0(line 24= ), > again using a file handle made from a socket. > > When I run the program, the following output is produced: > > =C2=A0 =C2=A0Listening at ('127.0.0.1', 1060) > =C2=A0 =C2=A0Accepted connection from ('127.0.0.1', 49938) > =C2=A0 =C2=A0Traceback (most recent call last): > =C2=A0 =C2=A0File "pickles.py", line 24, in > =C2=A0 =C2=A0 =C2=A0 =C2=A0d =3D pickle.load( s_fh ) > =C2=A0 =C2=A0File "/usr/local/lib/python2.7/pickle.py", line 1378, in loa= d > =C2=A0 =C2=A0 =C2=A0 =C2=A0return Unpickler(file).load() > =C2=A0 =C2=A0File "/usr/local/lib/python2.7/pickle.py", line 857, in load > =C2=A0 =C2=A0 =C2=A0 =C2=A0key =3D read(1) > =C2=A0 =C2=A0File "/usr/local/lib/python2.7/socket.py", line 380, in read > =C2=A0 =C2=A0 =C2=A0 =C2=A0data =3D self._sock.recv(left) > =C2=A0 =C2=A0socket.error: [Errno 107] Transport endpoint is not connecte= d > > I'm at a loss, can anyone provide any guidance? > > Thanks, > > Roger Alexander > > =C2=A01 =C2=A0import pickle > =C2=A02 =C2=A0import socket, sys > =C2=A03 > =C2=A04 =C2=A0s =3D socket.socket(socket.AF_INET, socket.SOCK_STREAM) > =C2=A05 > =C2=A06 =C2=A0HOST =3D sys.argv.pop() if len(sys.argv) =3D=3D 3 else '127= .0.0.1' > =C2=A07 =C2=A0PORT =3D 1060 > =C2=A08 > =C2=A09 =C2=A0if sys.argv[1:] =3D=3D ['server']: > 10 > 11 =C2=A0 =C2=A0 =C2=A0s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADD= R, 1) > 12 =C2=A0 =C2=A0 =C2=A0s.bind((HOST, PORT)) > 13 =C2=A0 =C2=A0 =C2=A0s.listen(1) > 14 > 15 =C2=A0 =C2=A0 =C2=A0print 'Listening at', s.getsockname() > 16 > 17 =C2=A0 =C2=A0 =C2=A0sc, sockname =3D s.accept() > 18 > 19 =C2=A0 =C2=A0 =C2=A0print 'Accepted connection from', sockname > 20 > 21 =C2=A0 =C2=A0 =C2=A0sc.shutdown(socket.SHUT_WR) [Haven't done any network programming, so please excuse the naivete of this suggestion.] Have you tried removing line #21 and/or #32? http://docs.python.org/library/socket.html#socket.socket.shutdown : "socket.shutdown(how) - Shut down one or both halves of the connection. [...] Depending on the platform, shutting down one half of the connection can also close the opposite half" Cheers, Chris -- http://blog.rebertia.com > 22 =C2=A0 =C2=A0 =C2=A0sf =3D s.makefile( "rb" ) > 23 > 24 =C2=A0 =C2=A0 =C2=A0d =3D pickle.load(sf) > 25 > 26 =C2=A0 =C2=A0 =C2=A0sc.close() > 27 =C2=A0 =C2=A0 =C2=A0s.close() > 28 > 29 =C2=A0elif sys.argv[1:] =3D=3D ['client']: > 30 > 31 =C2=A0 =C2=A0 =C2=A0s.connect((HOST, PORT)) > 32 =C2=A0 =C2=A0 =C2=A0s.shutdown(socket.SHUT_RD) > 42 =C2=A0 =C2=A0 =C2=A0pickle.dump( d, sf, pickle.HIGHEST_PROTOCOL ) > 43 > 44 =C2=A0 =C2=A0 =C2=A0s.close()