[newbie] trying socket as a replacement for nc

Christian Gollwitzer auriocus at gmx.de
Fri Dec 13 00:43:27 CET 2013

Am 12.12.13 00:08, schrieb Jean Dubois:
> 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 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:

> import socket
> host = ''
> port = 7000
> size = 10

The socket library advises to use a small power of two like 1024; 10 
seems very small.

> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.connect((host,port))
> s.send('*IDN?')
\n or \r\n is missing, you found this by yourself - look into the device 
manual, which one is correct

> data = s.recv(size)

It may be, that you simply need to wait for some time after the write, 
before you read. And then before the device is ready, you close the 
connection. If this is the case, try waiting a short time in between and 
use socket.sendall() instead of socket.send()

> s.close()

Maybe you need to read twice?

> Can anyone here tell me how to do it properly?

The most proper way is to use asynchronous IO; never done this in python 
before, check this:



More information about the Python-list mailing list