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


Groups > comp.lang.python > #61757

Re: [newbie] trying socket as a replacement for nc

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 <drsalists@gmail.com>
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 <d05fddc8-ad6c-488b-b0d2-ad5257b2aac8@googlegroups.com>
References <efae1b3c-c9c5-4ef4-8e94-7fbe8716ab53@googlegroups.com> <mailman.3939.1386804043.18130.python-list@python.org> <d05fddc8-ad6c-488b-b0d2-ad5257b2aac8@googlegroups.com>
Date Thu, 12 Dec 2013 13:23:22 -0800
Subject Re: [newbie] trying socket as a replacement for nc
From Dan Stromberg <drsalists@gmail.com>
To Jean Dubois <jeandubois314@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4026.1386883409.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Thu, Dec 12, 2013 at 12:28 AM, Jean Dubois <jeandubois314@gmail.com> wrote:
> 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 <jeandu...@gmail.com> wrote:
>>
>> I have an ethernet-rs232 adapter which allows me to connect to a measurement 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 socket.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 relevant IP stacks and communications equipment feel like for the sake of performance or reliability.
>>
>>
>> The confusing thing about this is, it won't be done on every transmission - in fact, it'll probably happen rather seldom unless you're on a heavy loaded 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 assuming 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 = '10.128.59.63'
port = 7000
size = 10
socket = socket_mod.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((host,port))
bufsock = bufsock_mod.bufsock(socket)
bufsock.send('*IDN?')
data = bufsock.recv(size)
bufsock.close()
print 'Received:', data

You might look over
http://stackoverflow.com/questions/19918307/retrieve-file-information-located-on-a-different-application-server-using-python/19918706#19918706
for a more complete example.

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


Thread

[newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-11 15:08 -0800
  Re: [newbie] trying socket as a replacement for nc Dan Stromberg <drsalists@gmail.com> - 2013-12-11 15:20 -0800
    Re: [newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-12 00:28 -0800
      Re: [newbie] trying socket as a replacement for nc Dan Stromberg <drsalists@gmail.com> - 2013-12-12 13:23 -0800
        Re: [newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-12 19:23 -0800
          Re: [newbie] trying socket as a replacement for nc Dan Stromberg <drsalists@gmail.com> - 2013-12-12 19:32 -0800
            Re: [newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-13 03:03 -0800
            Re: [newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-13 03:56 -0800
          Re: [newbie] trying socket as a replacement for nc Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-13 08:35 +0000
            Re: [newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-13 04:20 -0800
              Re: [newbie] trying socket as a replacement for nc rusi <rustompmody@gmail.com> - 2013-12-13 09:04 -0800
              Re: [newbie] trying socket as a replacement for nc rusi <rustompmody@gmail.com> - 2013-12-13 09:09 -0800
                Re: [newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-14 05:11 -0800
            Re: [newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-14 05:14 -0800
              Re: [newbie] trying socket as a replacement for nc Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-14 13:29 +0000
        Re: [newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-16 01:40 -0800
          Re: [newbie] trying socket as a replacement for nc Ervin Hegedüs <airween@gmail.com> - 2013-12-16 11:04 +0100
    Re: [newbie] trying socket as a replacement for nc Grant Edwards <invalid@invalid.invalid> - 2013-12-12 14:16 +0000
      Re: [newbie] trying socket as a replacement for nc Chris Angelico <rosuav@gmail.com> - 2013-12-13 01:23 +1100
      Re: [newbie] trying socket as a replacement for nc Dan Stromberg <drsalists@gmail.com> - 2013-12-12 13:27 -0800
        Re: [newbie] trying socket as a replacement for nc Grant Edwards <invalid@invalid.invalid> - 2013-12-13 16:06 +0000
          Re: [newbie] trying socket as a replacement for nc Dan Stromberg <drsalists@gmail.com> - 2013-12-14 17:24 -0800
            Re: [newbie] trying socket as a replacement for nc Grant Edwards <invalid@invalid.invalid> - 2013-12-15 15:15 +0000
              Re: [newbie] trying socket as a replacement for nc Roy Smith <roy@panix.com> - 2013-12-15 10:51 -0500
                Re: [newbie] trying socket as a replacement for nc Chris Angelico <rosuav@gmail.com> - 2013-12-16 03:04 +1100
                Re: [newbie] trying socket as a replacement for nc Roy Smith <roy@panix.com> - 2013-12-15 12:44 -0500
                Re: [newbie] trying socket as a replacement for nc Grant Edwards <invalid@invalid.invalid> - 2013-12-15 22:42 +0000
                Re: [newbie] trying socket as a replacement for nc Chris Angelico <rosuav@gmail.com> - 2013-12-16 09:48 +1100
      Re: [newbie] trying socket as a replacement for nc Chris Angelico <rosuav@gmail.com> - 2013-12-13 08:58 +1100
        Re: [newbie] trying socket as a replacement for nc Grant Edwards <invalid@invalid.invalid> - 2013-12-13 16:10 +0000
          Re: [newbie] trying socket as a replacement for nc Chris Angelico <rosuav@gmail.com> - 2013-12-14 03:19 +1100
            Re: [newbie] trying socket as a replacement for nc Grant Edwards <invalid@invalid.invalid> - 2013-12-13 16:57 +0000
              Re: [newbie] trying socket as a replacement for nc Chris Angelico <rosuav@gmail.com> - 2013-12-14 04:05 +1100
        Re: [newbie] trying socket as a replacement for nc 88888 Dihedral <dihedral88888@gmail.com> - 2013-12-16 04:38 -0800
          Re: [newbie] trying socket as a replacement for nc Chris Angelico <rosuav@gmail.com> - 2013-12-16 23:59 +1100
          Re: [newbie] trying socket as a replacement for nc Roy Smith <roy@panix.com> - 2013-12-16 09:03 -0500
            Re: [newbie] trying socket as a replacement for nc 88888 Dihedral <dihedral88888@gmail.com> - 2013-12-18 23:20 -0800
      Re: [newbie] trying socket as a replacement for nc Dave Angel <davea@davea.name> - 2013-12-12 19:52 -0500
  Re: [newbie] trying socket as a replacement for nc Conor Hughes <conorh@conorh.net> - 2013-12-11 15:38 -0800
    Re: [newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-12 00:08 -0800
      Re: [newbie] trying socket as a replacement for nc Chris Angelico <rosuav@gmail.com> - 2013-12-12 19:21 +1100
        Re: [newbie] trying socket as a replacement for nc Jean Dubois <jeandubois314@gmail.com> - 2013-12-12 01:21 -0800
          Re: [newbie] trying socket as a replacement for nc Alister <alister.ware@ntlworld.com> - 2013-12-12 14:05 +0000
          Re: [newbie] trying socket as a replacement for nc Alister <alister.ware@ntlworld.com> - 2013-12-12 14:05 +0000
            Re: [newbie] trying socket as a replacement for nc Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-12 14:34 +0000
  Re: [newbie] trying socket as a replacement for nc Christian Gollwitzer <auriocus@gmx.de> - 2013-12-13 00:43 +0100

csiph-web