[Tutor] Unpickling data after passing over the network

Adam Pridgen atpridgen at mail.utexas.edu
Sun Feb 11 07:10:44 CET 2007


Hello,

I am having problems with unpickling data after I pass the data through 
a socket.   Right now, I am pickling a basic string base 64 encoding and 
sending the data over the network.  After the recipient client/server 
receives the data, it is decoded and then unpickled.  The unpickling 
fails with an EOFError, and I am not sure why.  I have tested the my 
process works between functions.  I have not been able to find anything 
on google relating to this problem.  Right now I am using Python 2.4.3 
on a 64bit Linux box.  Below is a stack trace and below that is the 
script.  To run the script start the server in one command line: python 
BegPython.py server and in another shell start the client with python 
BegClient.py client . Thanks in advance for your help.  -ds

Traceback (most recent call last):
  File "BegClient.py", line 77, in ?
    elif sys.argv[1].lower() == "client": BegClient()
  File "BegClient.py", line 41, in BegClient
    data = DeserialDecode(recv_basic(mySocket))
  File "BegClient.py", line 23, in DeserialDecode
    return cPickle.loads(base64.decodestring(data))
EOFError

BegClient.py:

# To Run client server start the server then the client:
# python BegClient.py Server
# python BegClient.py Client

def Test():
    import cPickle, base64
    str = "This is one thing that I just do not understand, \
            but I will give it a shot"
    print str
    str = SerialEncode(str)
    print str
    str = DeserialDecode(str)
    print str

def SerialEncode(data):
    import cPickle, base64
    return base64.encodestring(cPickle.dumps(data,2))

def DeserialDecode(data):
    import cPickle, base64
    return cPickle.loads(base64.decodestring(data))

def recv_basic(the_socket):
    total_data=[]
    while True:
        data = the_socket.recv(8192)
        print data
        if data != "": break
        total_data.append(data)
    print "Finished Recving data"
    return ''.join(total_data)
   
def BegClient():
   
    import socket, cPickle
    mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
   
    mySocket.connect ( ( "", 64268 ) )
    data = DeserialDecode(recv_basic(mySocket))
    print data
   
    mySocket.send( SerialEncode("Hello!"))
    data = DeserialDecode(recv_basic(mySocket))
    print data
   
    mySocket.send( SerialEncode("Bye!"))
    print data
    mySocket.close()

def BegServer():
    import socket, sys, __builtin__
    def myhook(code):
        return code;
    sys.displayhook = myhook
   
    mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
    mySocket.bind ( ( '', 64268 ) )
    mySocket.listen ( 1 )
    i = 0
   
    import cPickle
   
    while i < 10:
       i += 1
       channel, details = mySocket.accept()
       print 'We have opened a connection with', details
       channel.send ( SerialEncode("What's up"))
       code = DeserialDecode(recv_basic(channel))
       print code
      
      
if __name__ == "__main__":
    import sys
    if sys.argv[1].lower() == "server": BegServer()
    elif sys.argv[1].lower() == "client": BegClient()
    else:
        Test()


More information about the Tutor mailing list