I am new to Mininet and I am trying to find a way to use a script in python to execute a few tests using Mininet. More precisely I want to build topology and send a few xmlrpc
request from one host to another. I've recently found how to use mininet API
. The problem is that I can't connect to the server from the client o get a response, I've tested using telnet 10.0.0.x 12345
command from the client host and got a connection denial
. Here is the code:
topology_test.py
import subprocess
from time import time, sleep
from signal import SIGINT
from xmlrpc.server import SimpleXMLRPCServer
import xmlrpc.client as cl
from mininet.util import pmonitor
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel, info
from mininet.cli import CLI# mininet topology
class MyTopology(Topo):def build(self, n=2):switch = self.addSwitch("s1")embedder = self.addHost("embedder")self.addLink(embedder, switch)# Python's range(N) generates 0..N-1for h in range(n):host = self.addHost("server%s" % (h + 1))self.addLink(host, switch)def simpleTest():"Create and test a simple network"output = subprocess.run(["sudo", "mn", "-c"])print(output)seconds = 10topo = MyTopology(n=4)net = Mininet(topo)net.start()print("Dumping host connections")dumpNodeConnections(net.hosts)print("Testing network connectivity")net.pingAll()h1 = net.get("server1")p1 = h1.popen("python3 myServer.py -i %s &" % h1.IP())h2 = net.get("embedder")h2.cmdPrint("telnet 10.0.0.2 12345")# CLI(net)p1.terminate()net.stop()if __name__ == "__main__":# Tell mininet to print useful informationsetLogLevel("info")simpleTest()
server.py
from xmlrpc.server import SimpleXMLRPCServer
import socket
import string
import socketserver
import os
import sys
from subprocess import Popen, PIPEx = int(sys.argv[1])
tries = 0class SimpleThreadedXMLRPCServer(socketserver.ThreadingMixIn, SimpleXMLRPCServer):allow_reuse_address = Trueclass Functions:# var_str = "yyyaaa"def guess_number(self, n):global triestries = tries + 1if n > x:msg = "Number is too high"if n < x:msg = "Number is too low"if n == x:msg = "You found the number in " + str(tries) + "tries. Congratulations!"tries = 0return msg# def str_get():# return var_strif __name__ == "__main__":port = 8100server = SimpleThreadedXMLRPCServer(("", port))server.register_instance(Functions())print("Serever listening...")server.serve_forever()
client.py
import xmlrpc.client as claddr = "10.0.0.2"
port = 8100
# rhost = xmlrpclib.Server("http://" + addr + ":" + str(port))
rhost = cl.Server("http://" + addr + ":" + str(port))
# msg = rhost.str_get()
# print(msg)
# inp = ""
print("Guess a number between 1 and 100")
print()
while 1:# msg = rhost.str_get()# print(msg)inp = input(">")msg = rhost.guess_number(int(inp))print(msg)if "found" in msg:sys.exit(0)
execution of server/client scripts to hosts
host1.sendCmd("python3 server.py")
host2.cmdPrint("python3 client.py")
If I run everything from mininet CLI
with different xterm
windows working fine but when using the mininet API
nothing works properly, I've also tried some different client/server scripts, and sometimes it was working. Actually, it worked only in the first run. So I was thinking maybe there is a way to use subprocess
to open xterm
windows and then run a command inside xterm
with some pipe subprocess
. I want everything to be run through a python script, not by hand. Can anyone give me any help?