Programming Guide

Table Of Contents
SIGLENT
SVA1000X Programming Guide 79
4.2 Examples of Using Sockets/Telnet
4.2.1 Example of Python
Python is an interpreted programming language that lets you work quickly and is very
portable.Python has a low-level networking module that provides access to the socket
interface.Python scripts can be written for sockets to do a variety of test and measurements
tasks.
Environment: Win7 32bit system, Python v2.7.5
The functions of this example: Open a socket, sends a query, and closes the socket. It
does this loop 10 times.
Below is the code of the script:
#!/usr/bin/env python
#-*- coding:utf-8 *-
#-----------------------------------------------------------------------------
# The short script is a example that open a socket, sends a query,
# print the return message and closes the socket.
#-----------------------------------------------------------------------------
import socket # for sockets
import sys # for exit
import time # for sleep
#-----------------------------------------------------------------------------
remote_ip = "10.11.13.32" # should match the instrument’s IP address
port = 5025 # the port number of the instrument service
count = 0
def SocketConnect():
try:
#create an AF_INET, STREAM socket (TCP)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print ('Failed to create socket.')
sys.exit();
try:
#Connect to remote server
s.connect((remote_ip , port))
info = s.recv(4096)
print (info)
except socket.error:
print ('failed to connect to ip ' + remote_ip)
return s
def SocketQuery(Sock, cmd):
try :
#Send cmd string
Sock.sendall(cmd)
Sock.sendall(b'\n')
time.sleep(1)
except socket.error:
#Send failed
print ('Send failed')
sys.exit()
reply = Sock.recv(4096)
return reply
def SocketClose(Sock):
#close the socket
Sock.close()