Socket connection between python and C
roy at panix.com
Wed Feb 9 02:41:49 CET 2011
In article <mailman.23.1297213437.1633.python-list at python.org>,
"Williamson, Ross X. (Guest)" <Ross.X.Williamson.Guest at usap.gov>
> 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.
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.
Based on your statement that you're thinking of using "struct", my guess
is that XML would be overkill.
More information about the Python-list