471,872 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,872 software developers and data experts.

Client for server doing dispatcher_with_send

Hi all,

I have a server that uses the asyncore stuff with dispatcher_with_send in
order to send a bunch of pickled bytes to a client. The length of the
message is generally about 3000 bytes.

My problem is that I don't know how to write the client elegantly. I'm not
using asyncore or anything for the client -- just the socket module.

Currently, my code is working, but is inelegant, I think. here it is:

data = ""
newData = ""
while True:
newData = sock.recv(8192)
data += newData
if len(newData) < 512:

This works because the dispatcher_with_send() sends packets with 512 bytes
in them, unless there is less than 512 bytes to send.

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

Jul 18 '05 #1
1 1141
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_s end):
def send (self, data):
asyncore.dispatcher_with_send(struct.pack('>L', len(data))+data)

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

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

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Freddie | last post: by
2 posts views Thread by Richard Choate | last post: by
11 posts views Thread by Tom Leylan | last post: by
15 posts views Thread by Michael Rybak | last post: by
3 posts views Thread by Simon Brooke | last post: by
5 posts views Thread by Yossarian | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.