Understanding sockets?

Miki Tebeka tebeka at cs.bgu.ac.il
Sun Mar 23 04:55:29 EST 2003


Hello G,

> Still i would like to know what the problem is with the original code?
> I'm trying to learn about sockets.
> If anyone has any suggestions, i would really appreciate it.

My guess (apart from the missing imports) is that you get both pickle
objects in the same string and pickle gets only the first.
Try:
class Server(SocketServer.BaseRequestHandler):
    def parse_data(self, data):
        for i in range(1, len(data) + 1):
            try:
                obj = pickle.loads(data[:i])
                return obj, data[i:]
            except:
                pass
        raise ValueError
                
    def handle(self):
        msg = ''
        while 1:
            data = self.request.recv(1024)
            if not data:
                break
            msg += data
            while 1:
                try:
                    obj, msg = self.parse_data(msg)
                    print obj
                except ValueError:
                    break
        if msg:
            print pickle.loads(msg)


HTH.
Miki




More information about the Python-list mailing list