473,327 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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:
break
sock.close()

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?

Vic
Jul 18 '05 #1
1 1169
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:
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()

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Freddie | last post by:
Hi, I've been mangling python-irclib into an asyncore class, so it fits in nicely with the rest of my app. I ran into a problem with asyncore.dispatcher_with_send (Python 2.3.4), though. Not...
2
by: Richard Choate | last post by:
I'm writing an article and I need your informed opinions, so I ask you this: 1. Doesn't the web-enabled app generally run slower for one reason or another? 2. Isn't the fact that application...
11
by: Tom Leylan | last post by:
(I posted this in languages.vb also... I can't figure out where things go if you use a little of a lot of things) Hi all... I'm looking for an example (or a pointer to one) related to the...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
18
by: cjl | last post by:
Hey all: I know that it is silly in the age of Google to 'lose' something on the internet, but I recently checked out a project that had implemented a database with a subset of SQL in pure...
3
by: Simon Brooke | last post by:
I've been doing XSL transforms, converting XML to HTML, server side since 2000. In those days, clients which could do the transformation client side didn't exist, so whether to transform...
5
by: Yossarian | last post by:
I have a handheld running CE .NET 4.2 and I am using c# with framework 1.1 to develop a solution for syncing data that is on the handheld with the local pc. Our handheld cradles only support...
1
by: hocker | last post by:
I have created a client-server application using .NET remoting in C#. The client initiates a call to the server then subscribes to events from the server, using an abstract class which contains the...
22
by: Dan Rumney | last post by:
Hi all, I've been writing Javascript for quite a while now and have, of late, been writing quite a lot of AJAX and AJAX-related code. In the main, my dynamically generated pages are created...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.