Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55833 > unrolled thread

Socket connection between python and C

Started by"Williamson, Ross X. (Guest)" <Ross.X.Williamson.Guest@usap.gov>
First post2011-02-09 13:54 +1300
Last post2011-02-08 20:41 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Socket connection between python and C "Williamson, Ross X. (Guest)" <Ross.X.Williamson.Guest@usap.gov> - 2011-02-09 13:54 +1300
    Re: Socket connection between python and C Dan Stromberg <drsalists@gmail.com> - 2011-02-08 18:15 -0800
    Re: Socket connection between python and C Roy Smith <roy@panix.com> - 2011-02-08 20:41 -0500

#55833 — Socket connection between python and C

From"Williamson, Ross X. (Guest)" <Ross.X.Williamson.Guest@usap.gov>
Date2011-02-09 13:54 +1300
SubjectSocket connection between python and C
Message-ID<mailman.23.1297213437.1633.python-list@python.org>
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?

Cheers,

Ross

[toc] | [next] | [standalone]


#55900

FromDan Stromberg <drsalists@gmail.com>
Date2011-02-08 18:15 -0800
Message-ID<mailman.26.1297217727.1633.python-list@python.org>
In reply to#55833
On Tue, Feb 8, 2011 at 5:41 PM, Roy Smith <roy@panix.com> wrote:
> In article <mailman.23.1297213437.1633.python-list@python.org>,
>  "Williamson, Ross X. (Guest)" <Ross.X.Williamson.Guest@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.

[toc] | [prev] | [next] | [standalone]


#56056

FromRoy Smith <roy@panix.com>
Date2011-02-08 20:41 -0500
Message-ID<roy-5A75F0.20414908022011@news.panix.com>
In reply to#55833
In article <mailman.23.1297213437.1633.python-list@python.org>,
 "Williamson, Ross X. (Guest)" <Ross.X.Williamson.Guest@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.

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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web