Errno 111 Connection refused - Python Mininet API hosts client/server no connection?

2024/11/20 17:15:36

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?

Answer

Why not use mininet's python interface to achieve this? From your question it seems like you want to get the names of the nodes that sudo mn creates by default.

In that case why not just add the following to your python:

from mininet.topo import SingleSwitchTopo
from mininet.net import Mininet
from mininet.node import CPULimitedHost
from mininet.link import TCLinkif __name__ == '__main__':topo = SingleSwitchTopo()net = Mininet(topo=topo, host=CPULimitedHost, link=TCLink)net.start()nodes = net.items()node_names, _ = zip(*nodes)print(node_names)net.stop()

SingleSwitchTopo is the one that mn uses by default (two hosts connected to a single switch). net.items() gives you a tuple of the node's names and ids see here. Perhaps I misunderstand your question, but seems like trying to access it via subprocess when there is an API is overcomplicating an otherwise simple task.

https://en.xdnf.cn/q/118392.html

Related Q&A

Finding the Corners of the an array of coordinates

I have a 2D array of Coordinates in Numpy.My goal is to attempt to find the corners (as if it were a square). So the :Top left: smallest x, highest y Top right: largest x, largest y bottom left: smalle…

How to make a dictionary retain its sort order?

def positive(self):total = {}final = {}for word in envir:for i in self.lst:if word in i:if word in total:total[word] += 1else:total[word] = 1final = sorted(total, reverse = True)return totalThis return…

Counting line frequencies and producing output files

With a textfile like this:a;b b;a c;d d;c e;a f;g h;b b;f b;f c;g a;b d;fHow can one read it, and produce two output text files: one keeping only the lines representing the most often occurring couple …

Check if parent dict is not empty and retrieve the value of the nested dict

Lets suppose that I have a nested dictionary which looks like that:parent_dict = { parent_key: {child_key: child_value}How can I write the following code:if parent_dict.get(parent_key) is not None and …

List combinations in defined range

I am writing parallel rainbow tables generator using parallel python and multiple machines. So far, I have it working on a single machine. It creates all possible passwords, hashes them, saves to file.…

Python turtle drawing a symbol

import turtlewin=turtle.Screen()t = turtle.Turtle() t.width(5)#The vertical and horizontal lines t.left(90) t.forward(70) t.left(90) t.forward(20)t.left(90) t.forward(60) t.left(120) t.forward(35) t.b…

Display a countdown for the python sleep function in discord embed in python

hi all I am doing one discord bot I need to send one countdown its like a cooldown embed after every request I did this code but I dont know how to add this in my embedfor i in range(60,0,-1):print(f&q…

Bypass rate limit for requests.get

I want to constantly scrape a website - once every 3-5 seconds withrequests.get(http://www.example.com, headers=headers2, timeout=35).json()But the example website has a rate limit and I want to bypass…

ValueError when using if commands in function

Im creating some functions that I can call use keywords to call out specific functions,import scipy.integrate as integrate import numpy as npdef HubbleParam(a, model = "None"):if model == &qu…

Python consecutive subprocess calls with adb

I am trying to make a python script to check the contents of a database via adb. The thing is that in my code,only the first subprocess.call() is executed and the rest are ignored. Since i am fairly ne…