Socket connection between python and C
Irmen de Jong
irmen at -NOSPAM-xs4all.nl
Wed Feb 9 20:38:42 CET 2011
On 09-02-11 01:54, Williamson, Ross X. (Guest) 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?
For a binary protocol I recall once using the marshal module from Python
to send simple data structures over a stream to a C program on the other
end. The marshal protocol is simple and was parsed by the C code on the
other end. Look in marshal.c in Python's source.
Actually now I think of it, it was the other way around: there was a
little bit of minimalistic code in a web server written in C, that
encoded stuff in the marshal protocol, and it was the Python process
that simply used the marshal module to get objects out of it. But I
think it shouldn't be hard to make this 2-way if you make some
restrictions on the data types you use.
Binary protocols have their drawbacks though, and maybe it's best to go
for Json instead for reasons mentioned already in the other replies.
More information about the Python-list