Client for server doing dispatcher_with_send

Josiah Carlson jcarlson at uci.edu
Fri Oct 8 13:48:38 EDT 2004


> Is there a more elegant way to write this?  How can I determine that nothing
> more is being sent?

Write a better protocol.  A better protocol is easy (it can be done
better, but this is short and will do the job).

 - Josiah



from StringIO import StringIO

class my_dispatcher_with_send(asyncore.dispatcher_with_send):
    def send (self, data):
        asyncore.dispatcher_with_send(struct.pack('>L', len(data))+data)

def read(sock, length):
    s = StringIO()
    while s.tell() < length:
        s.write(sock.recv(length-s.tell()))
    return s.getvalue()

def receiver(sock):
    try:
        l = read(sock, 4)
        l = struct.unpack('>L', l)[0]
        r = read(sock, l)
        sock.close()
        return r
    except socket.error, why:
        sock.close()




More information about the Python-list mailing list