[Tutor] Unpickling data after passing over the network

Kent Johnson kent37 at tds.net
Sun Feb 11 14:04:41 CET 2007


Adam Pridgen wrote:
> 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 don't know what the problem is, but I suspect that the data is not 
arriving all at once at the client. I would put in some print statements 
so you can compare what you send to what you receive. (Adding the prints 
will change the timing; this may make it start to work. That in itself 
will be a clue.) Since you are just sending strings in this program, 
maybe you should strip out the base64 and pickle stuff for now and get 
it working with bare strings.

If you are trying to create a way to send objects over a socket you 
might want to look at what is already available. Pyro is well-regarded.
http://pyro.sourceforge.net/

Kent

> 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()
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list