Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.04; '"""': 0.07; 'aggregate': 0.07; 'paths': 0.07; 'reason,': 0.07; 'socket': 0.07; 'string': 0.09; 'assuming': 0.09; 'back.': 0.09; 'dan': 0.09; 'sake': 0.09; 'subject:trying': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'wrote': 0.14; 'adapter': 0.16; 'cc:name:python list': 0.16; 'discovery,': 0.16; 'measurement': 0.16; 'reliability.': 0.16; 'simple.': 0.16; 'sockets': 0.16; 'tcp': 0.16; 'thursday,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'split': 0.19; 'thu,': 0.19; 'examples': 0.20; 'subject:] ': 0.20; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'example.': 0.24; 'cc:2**0': 0.24; '(see': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'host': 0.29; 'am,': 0.29; "doesn't": 0.30; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'page.': 0.31; "skip:' 10": 0.31; 'jean': 0.31; 'allows': 0.31; 'probably': 0.32; 'linux': 0.33; 'skip:# 10': 0.33; 'could': 0.34; 'december': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'done': 0.36; 'url:org': 0.36; 'should': 0.36; 'performance': 0.37; 'e.g.': 0.38; 'pm,': 0.38; 'rather': 0.38; '12,': 0.39; 'bad': 0.39; 'system.': 0.39; 'equipment': 0.61; 'simple': 0.61; "you're": 0.61; 'complete': 0.62; 'happen': 0.63; 'more': 0.64; 'dear': 0.65; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'sample': 0.67; 'fact,': 0.69; 'heavy': 0.81; 'confusing': 0.84; 'seldom': 0.84; 'abstracts': 0.91; 'instrument': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=EM0b5xCm+eiC7JQD8fuIiBBNoPyRvLRyjG5AwS8Vnlo=; b=wufpMIAmrgUQxwNiRmdRyxXm34LH6T5HhRlzdDh1Anu3g0OSf8iYzC1pE4UqbD6TQa kr5+ozHWhrjWo+p85mgfDVGLzfdyT2MosStO/FeE4wkkJNx2JtPZtNmjDmrS9i6vvk66 XBSE5iOkVI/8UPp0rH2x+29BKte8I4rk/RySbLMRf04WgxPqbcgP8R1xc7tnKiwfI3Vx FYMuDkWtDfl1jQXGXcvzvydIw6S4FKngCGw9BXPU2MgyplwakBw+tJcYu8x5bgyV1fLH mY8xWUn8gdHjLYYBaNBnPwWl48N73hZoL9EV0VRi3diKAMQa8Uj4uJMNYjbZOytiIMeV wdlg== MIME-Version: 1.0 X-Received: by 10.180.205.138 with SMTP id lg10mr97509wic.30.1386883402677; Thu, 12 Dec 2013 13:23:22 -0800 (PST) In-Reply-To: References: Date: Thu, 12 Dec 2013 13:23:22 -0800 Subject: Re: [newbie] trying socket as a replacement for nc From: Dan Stromberg To: Jean Dubois Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 76 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386883409 news.xs4all.nl 2953 [2001:888:2000:d::a6]:33656 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61757 On Thu, Dec 12, 2013 at 12:28 AM, Jean Dubois wro= te: > On Thursday, December 12, 2013 12:20:36 AM UTC+1, Dan Stromberg wrote: >> On Wed, Dec 11, 2013 at 3:08 PM, Jean Dubois wrote= : >> >> I have an ethernet-rs232 adapter which allows me to connect to a measure= ment instrument by means of netcat on a linux system. >> >> >> e.g. entering nc 10.128.59.63 7000 >> >> allows me to enter e.g. >> >> *IDN? >> >> after which I get an identification string of the measurement instrument= back. >> >> I thought I could accomplish the same using the python module "socket" >> >> and tried out the sample program below which doesn't work however: >> >> >> >> Sockets reserve the right to split one socket.send() into multiple socke= t.recv()'s on the other end of the communication, or to aggregate multiple = socket.send()'s into a single socket.recv() - pretty much any way the relev= ant IP stacks and communications equipment feel like for the sake of perfor= mance or reliability. >> >> >> The confusing thing about this is, it won't be done on every transmissio= n - in fact, it'll probably happen rather seldom unless you're on a heavy l= oaded network or have some MTU issues (see Path MTU Discovery, and bear in = mind that paths can change during a TCP session). But writing your code as= suming it will never happen is a bad idea. >> >> >> >> For this reason, I wrote http://stromberg.dnsalias.org/~strombrg/bufsock= .html , which abstracts away these complications, and actually makes things= pretty simple. There are examples on the web page. >> >> >> >> HTH > > Dear Dan, > Could you copy paste here the code for your function I have to add to my = "program"? This is untested, but it should be something like the following: #!/usr/bin/env python """ A simple echo client """ import socket as socket_mod import bufsock as bufsock_mod host =3D '10.128.59.63' port =3D 7000 size =3D 10 socket =3D socket_mod.socket(socket.AF_INET, socket.SOCK_STREAM) socket.connect((host,port)) bufsock =3D bufsock_mod.bufsock(socket) bufsock.send('*IDN?') data =3D bufsock.recv(size) bufsock.close() print 'Received:', data You might look over http://stackoverflow.com/questions/19918307/retrieve-file-information-locat= ed-on-a-different-application-server-using-python/19918706#19918706 for a more complete example.