Applicable Versions

NetSim Standard

NetSim Pro


Applicable Releases
v14


NetSim provides run-time interfacing with Python so that users who are familiar with python can implement some parts of their algorithm in Python without having to modify the C source codes of NetSim. A lot of work related to machine learning, artificial intelligence, and specialized mathematical algorithms which can be used for networking research, can be carried out using Python code. NetSim offers Socket interfacing to interact with Python during runtime.

NetSim-Python socket interface

NetSim provides a TCP socket with which a socket program/application written in any programming language can establish a connection. The conversation can remain until the connection is terminated by either side. 

By default, NetSim uses the port 8999 in which it listens for any incoming connections. Client programs can connect to the NetSim socket using the loopback/IP address of the system running NetSim along with the port number 8999.

A client socket program written in Python can connect to the NetSim Server process in a similar way as depicted here using the loopback address 127.0.0.1 and port 8999.

A simple Netsim-python interfacing example


In this example, we will use NetSim interactive simulation to implement a simple internetworking example using python socket interfacing

Steps for performing the socket interfacing in NetSim
A network scenario in NetSim’s Internetwork with one router connected to two wired nodes.

Create a new python file with python code for socket interfacing. Any specific command can be used. Here in the given code, route command has been used.

import socket # for socket
import sys
import time
try:
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 print ("Socket successfully created.")
except socket.error as err:
 print ("Socket creation failed with error %s" %(err))
 # default port for socket
port = 8999
try:
 host_ip = socket.gethostbyname('127.0.0.1')
except socket.gaierror:
 # this means could not resolve the host
 print ("Error resolving host.")
 sys.exit()
 # connecting to the server
s.connect((host_ip, port))
print ("Connection established to NetSim.")
name='W2'
name = name + '\0'
s.send(name.encode())
command = 'route print'
command = command + '\0'
s.send(command.encode())
resp = s.recv(1024).decode('utf-8')
cont = '__continue__'
while cont not in resp:
 resp = resp + s.recv(1024).decode('utf-8')
print ("Received:", resp)
s.close()


In the Netsim runtime interaction window present under "Options" at the top ribbon of NetSim GUI, set Interactive simulation to true and then click on ‘OK’.

Run the application in Run simulation window.

Simulation (NetSimCore.exe) will start running and will display a message “waiting for first client to connect” as shown below

Now run the python file created ‘socketdemo.py’ using cmd.

The user can observe the results after the simulation, as shown below. 


Related examples:
NetSim-Socket-Interfacing-v14.1
How-to-pass-parameters-from-NetSim-code-to-Python-code-during-Simulation
Configuring access control lists (ACL) in NetSim through python based socket interface