Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3583

Re: Pickling over a socket

References <61890800-f81a-4a1e-8905-a0237407f016@a21g2000prj.googlegroups.com>
Date 2011-04-19 12:21 -0700
Subject Re: Pickling over a socket
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.579.1303240920.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Apr 19, 2011 at 11:53 AM, Roger Alexander <rtalexander@mac.com> wrote:
> 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  (line 24),
> again using a file handle made from a socket.
>
> When I run the program, the following output is produced:
>
>    Listening at ('127.0.0.1', 1060)
>    Accepted connection from ('127.0.0.1', 49938)
>    Traceback (most recent call last):
>    File "pickles.py", line 24, in <module>
>        d = pickle.load( s_fh )
>    File "/usr/local/lib/python2.7/pickle.py", line 1378, in load
>        return Unpickler(file).load()
>    File "/usr/local/lib/python2.7/pickle.py", line 857, in load
>        key = read(1)
>    File "/usr/local/lib/python2.7/socket.py", line 380, in read
>        data = self._sock.recv(left)
>    socket.error: [Errno 107] Transport endpoint is not connected
>
> I'm at a loss, can anyone provide any guidance?
>
> Thanks,
>
> Roger Alexander
>
>  1  import pickle
>  2  import socket, sys
>  3
>  4  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>  5
>  6  HOST = sys.argv.pop() if len(sys.argv) == 3 else '127.0.0.1'
>  7  PORT = 1060
>  8
>  9  if sys.argv[1:] == ['server']:
> 10
> 11      s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> 12      s.bind((HOST, PORT))
> 13      s.listen(1)
> 14
> 15      print 'Listening at', s.getsockname()
> 16
> 17      sc, sockname = s.accept()
> 18
> 19      print 'Accepted connection from', sockname
> 20
> 21      sc.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      sf = s.makefile( "rb" )
> 23
> 24      d = pickle.load(sf)
> 25
> 26      sc.close()
> 27      s.close()
> 28
> 29  elif sys.argv[1:] == ['client']:
> 30
> 31      s.connect((HOST, PORT))
> 32      s.shutdown(socket.SHUT_RD)
<snip>
> 42      pickle.dump( d, sf, pickle.HIGHEST_PROTOCOL )
> 43
> 44      s.close()

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Pickling over a socket Roger Alexander <rtalexander@mac.com> - 2011-04-19 11:53 -0700
  Re: Pickling over a socket Chris Rebert <clp2@rebertia.com> - 2011-04-19 12:21 -0700
  Re: Pickling over a socket Chris Angelico <rosuav@gmail.com> - 2011-04-20 05:29 +1000
  Re: Pickling over a socket Dan Stromberg <drsalists@gmail.com> - 2011-04-19 12:30 -0700
  Re: Pickling over a socket Chris Angelico <rosuav@gmail.com> - 2011-04-20 05:37 +1000
    Re: Pickling over a socket Roger Alexander <rtalexander@mac.com> - 2011-04-19 15:27 -0700
      Re: Pickling over a socket Jean-Paul Calderone <calderone.jeanpaul@gmail.com> - 2011-04-19 19:28 -0700
        Re: Pickling over a socket Bastian Ballmann <balle@chaostal.de> - 2011-04-20 08:44 +0200
        Re: Pickling over a socket Chris Angelico <rosuav@gmail.com> - 2011-04-20 16:59 +1000
        Re: Pickling over a socket Bastian Ballmann <balle@chaostal.de> - 2011-04-20 09:34 +0200
          Re: Pickling over a socket Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-20 10:25 +0200
            [OT] Re: Pickling over a socket Bastian Ballmann <balle@chaostal.de> - 2011-04-20 10:59 +0200
        Re: Pickling over a socket Chris Angelico <rosuav@gmail.com> - 2011-04-20 19:26 +1000
        Re: Pickling over a socket Bastian Ballmann <balle@chaostal.de> - 2011-04-20 11:41 +0200

csiph-web