Q:

send SCPI command through python socket module

I am using python's socket module to send a simple SCPI command to my Keysight device, but I get a blank response. Does anyone know what I am doing wrong?
Here is my simple script.
import socket
s = socket.socket()
print("connecting")
s.connect(("192.168.25.7",5024))
print("sending")
s.settimeout(10)
s.send("*IDN?\n")
print(s.recv(2048))

I expect this script to return
➜  ~ python hack_na.py
connecting
sending
Agilent Technologies,E5063A,MY54100104,A.03.00
➜  ~ 

But if I run the script, I get this output instead.
➜  ~ python hack_na.py
connecting
sending

➜  ~ 

I can confirm that I get the desigred output with telnet.
➜  ~ telnet 192.168.25.7 5024
Trying 192.168.25.7...
Connected to 192.168.25.7.
Escape character is '^]'.

SCPI> *IDN?
Agilent Technologies,E5063A,MY54100104,A.03.00
SCPI>
telnet> Connection closed.

Thank you all in advance.

A:

These two versions are not the same.
You're not seeing any output in the first case because you're not actually sending a complete message from the socket to the server. The telnet protocol uses \r\n to end lines, not just \n. So you'd need to do s.send("*IDN?\r\n").
But really, you should use a better tool for the job. Python's socket module is  just direct bindings to the BSD socket interface, usually used for building low-level networking applications. As such, you'll need to worry about annoying details like the line-endings yourself. A better alternative is to use a higher-level library, more tailored for your purpose. telnetlib is a builtin module for operating as a telnet client, or you could use a third-party library explicitly for SCPI.

