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


Groups > comp.lang.python > #55900

Re: Socket connection between python and C

Path csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <drsalists@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.212.46': 0.03; 'received:mail-vw0-f46.google.com': 0.03; '(using': 0.05; 'c/c++': 0.05; 'pointer': 0.05; 'socket': 0.05; 'approach.': 0.07; 'compiler': 0.07; 'happily': 0.07; 'json': 0.07; 'though.': 0.07; 'python': 0.08; 'cool.': 0.09; 'hierarchical': 0.09; 'i/o': 0.09; 'struct': 0.09; 'pm,': 0.12; 'tue,': 0.12; 'binary': 0.13; 'slightly': 0.13; 'wrote:': 0.14; 'ideally': 0.15; 'protocol': 0.15; 'subject:python': 0.15; 'c/c++.': 0.16; 'downside': 0.16; 'low-level': 0.16; 'socket.': 0.16; 'subject:Socket': 0.16; 'subject:between': 0.16; '\xa0for': 0.16; 'feb': 0.16; 'cc:no real name:2**0': 0.17; 'written': 0.20; 'cc:2**0': 0.21; '(or': 0.21; 'guess': 0.21; '(which': 0.22; 'trying': 0.22; 'buffer': 0.23; 'similar,': 0.23; 'header:In-Reply-To:1': 0.23; '(and': 0.23; 'received:209.85.212': 0.24; 'cc:addr:python-list': 0.24; 'possible,': 0.24; 'somewhat': 0.24; 'tests': 0.25; 'language': 0.26; 'module': 0.27; "i'm": 0.28; "can't": 0.28; 'testing': 0.28; 'least': 0.29; 'implement': 0.29; 'message-id:@mail.gmail.com': 0.29; 'all,': 0.30; 'server': 0.30; 'cc:addr:python.org': 0.30; 'extent': 0.31; 'worry': 0.31; 'thinking': 0.32; 'however,': 0.32; 'bit': 0.32; "won't": 0.32; 'probably': 0.32; 'option': 0.33; 'depends': 0.33; 'prepared': 0.33; 'preserve': 0.34; 'things': 0.34; 'there': 0.34; 'where': 0.35; 'another': 0.35; 'using': 0.35; 'raw': 0.36; 'statement': 0.36; 'test': 0.37; 'though': 0.37; "i'd": 0.38; 'received:209.85': 0.38; 'received:google.com': 0.38; 'might': 0.38; 'data': 0.38; 'should': 0.38; 'corner': 0.39; 'xml': 0.39; 'subject:: ': 0.39; 'some': 0.39; 'pretty': 0.39; 'could': 0.40; "it's": 0.40; 'side': 0.60; 'your': 0.61; 'give': 0.61; 'easy': 0.61; 'cost': 0.62; 'simple': 0.63; 'dear': 0.63; 'kinds': 0.68; 'benefit': 0.68; 'become': 0.70; 'paste': 0.74; 'article': 0.74; 'long-term': 0.79; 'beat.': 0.84; 'client?': 0.84; 'envision': 0.84; 'fast,': 0.84; 'packed': 0.84; 'troubles': 0.84; 'ascii.': 0.91; 'casting': 0.91; 'framing': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=HcLWIiK18Otfse9WVQP6SpB+iFgZE+fSROf3vKdfumc=; b=GGX1yN8vKsnx/jv+rRYulnbZFM1NBrI8dAOaXvuEOwFqzbsa3RpaG+sBaNvWalm2iZ 1EG70orKzuoJbTVMGCjqh7LcQjRkQLK75WRekFW7v7As2zMhQijnQ2nQSn7iiTbLC7IX 6eZBtN+XZPg/T1aRCAXof1vdCIyUVkRkyhROc=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=xe7qnxPahOI9fFI0htPnPfwmmg9gc77cgPtLjD4o/Hpie+PKiNUtmlTKEcLHbwMWll roi3i9vPyb8DJNkB1AFwB1XkEzWz1IO4siRCihGSh7KXEHEJnL2Y+DBBfaDY4hqHAT/A QjDMbTZlMqq2RyOB4I1pcrczcR4kF0PbQzAUg=
MIME-Version 1.0
In-Reply-To <roy-5A75F0.20414908022011@news.panix.com>
References <mailman.23.1297213437.1633.python-list@python.org> <roy-5A75F0.20414908022011@news.panix.com>
Date Tue, 8 Feb 2011 18:15:25 -0800
Subject Re: Socket connection between python and C
From Dan Stromberg <drsalists@gmail.com>
To Roy Smith <roy@panix.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.26.1297217727.1633.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 82.94.164.166
X-Trace 1297217728 news.xs4all.nl 81478 [::ffff:82.94.164.166]:35070
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:55900

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web