On Tue, Feb 8, 2011 at 5:41 PM, Roy Smith <r...@panix.com> wrote: > In article <mailman.23.1297213437.1633.python-l...@python.org>, > "Williamson, Ross X. (Guest)" <ross.x.williamson.gu...@usap.gov> > wrote: > >> Dear All, >> >> I'm trying to implement a server/client system where the server is written in >> python and the client has to be written in c/c++. I can happily send simple >> text through the socket. Ideally I would like make say a struct (using python >> struct library) - and then read that in using C. Is there a better way to >> package data on the server in python to send down a socket to a C client? >> XML? Pickle? > > Depends on what you are trying to accomplish. > > If your goal is for the communication to be as efficient as possible, > sending raw packed binary with the struct module on the python side, and > casting the i/o buffer pointer to a struct pointer on the C/C++ side > probably can't be beat. The downside is you need to worry about > low-level things like padding, overflow, and endian-ness yourself.
Yes, this is fast, and yes, this likely won't be a good long-term option if you envision someday using even slightly exotic (or new) hardware - even using a different compiler on the same hardware could lead to troubles with this approach. However, socket.htons and related functions are a pretty good (and somewhat similar, though without most of the problems) option. > If you want to give up a little bit of efficiency in return for a huge > amount of convenience, I'd go with JSON. For the kinds of things you > might be thinking about using the struct module for, it's just peachy. > Hugely portable (libraries for every language imaginable), easy to use, > and not grossly inefficient. There's also BSON, which will be a bit > more efficient at the cost of some portability. JSON's cool. > Based on your statement that you're thinking of using "struct", my guess > is that XML would be overkill. Yes, XML's a bit more heavyweight than JSON. Also, if your data need not ever become hierarchical (which is another architectural choice that could paint you into a corner a bit), or you are prepared to add framing to your protocol if/when the time comes, you can just use ASCII. This has the benefit of allowing you to test your server with telnet. Actually, JSON and XML should preserve telnet-based server testing as well, at least to some extent - you might have to cut and paste your tests more though. -- http://mail.python.org/mailman/listinfo/python-list