使用python创建拓扑

使用python创建拓扑,python,mininet,topology,Python,Mininet,Topology,我想使用python API构建一个mininet拓扑。我的拓扑结构应该有2个控制器、3个交换机,每个交换机将有多个主机连接到它(连接的主机数分别为2、4、1)。代码如下: #!/usr/bin/python

我想使用python API构建一个mininet拓扑。我的拓扑结构应该有2个控制器、3个交换机,每个交换机将有多个主机连接到它(连接的主机数分别为2、4、1)。代码如下:

#!/usr/bin/python                                                                                                                                                                   

import time
from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel, info


def net():

    net = Mininet(controller=RemoteController, switch=OVSKernelSwitch)
    c1 = net.addController('c1', controller=RemoteController, ip="127.0.0.1", port=6653)
    c2 = net.addController('c2', controller=RemoteController, ip="127.0.0.1", port=7753)

    s_n = 3 #number of switches
    c_n = 2 #number of controllers
    hosts = []
    amount = [2, 4, 1] # number of hosts that each switch has

    info( "*** Creating switches\n" )
    switches = [ net.addSwitch( 's%s' % s ) for s in range( 1, s_n ) ]

    index1 = 0 
    L_in = 0
    index2 = 0

    info( "*** Creating hosts\n" )
    for s in switches:
        L_in = (L_in + 1)
        if L_in <= len(amount):

            index1 = (index2 + 1)
            index2 = (index1 + amount[L_in]) - 1
            hosts = [ net.addHost( 'h%s' % n ) for n in ( index1, index2 ) ]
            for h in hosts:
                net.addLink( s, h )
        else:
            break

    # Wire up switches
    last = None
    for switch in switches:
        if last:
            net.addLink( last, switch )
        last = switch

    net.build()
    c1.start()
    c2.start()

    for sw in switches[:c_n]:
        sw.start( [c1] )

    for sw in switches[c_n:]:   
        sw.start( [c2] )

    net.start()
    CLI( net )
    net.stop()


if __name__ == '__main__':
    setLogLevel( 'info' )
    net()

这里有很多问题

范围(1,n)
生成从
1
no
n-1
n
的数字

您定义的函数
net
将覆盖模块
net
的上一个(导入的?)定义。称之为
make\u net
或其他什么

index2
这样的显式循环索引几乎总是一个坏主意,就像
index2
那样的非描述性名称一样

像这样的东西应该会让你产生这样的想法:

n_switches = 3
hosts_per_switch = [2, 4, 1]

switches = [addSwitch("s%d" % n + 1) for n in range(n_switches)]

for (switch, number_of_hosts) in zip(switches, hosts_per_switch):  # Pair them.
    hosts = [addHost("h%d" % n + 1) for n in range(number_of_hosts)]
    for host in hosts:
        addLink(switch, host)

谢谢我明白你的意思,我编辑了我的代码。虽然它似乎工作得更好,但每次将新主机添加到交换机时,数字都会被重置。我的意思是我希望有(H1H2代表s1),(H3H4H5H6代表s2)和(h7代表s3),但我在输出中有(H1H2代表s1),(H1H2H3H4代表s2)和(h1代表s3),这使得为每一个都分配IP变得很困难。我在第二个代表之后使用了一个z。首先z=0,但是这个z等于之前的主机数量,我使用z+n+1代替for循环中的n+1。
n_switches = 3
hosts_per_switch = [2, 4, 1]

switches = [addSwitch("s%d" % n + 1) for n in range(n_switches)]

for (switch, number_of_hosts) in zip(switches, hosts_per_switch):  # Pair them.
    hosts = [addHost("h%d" % n + 1) for n in range(number_of_hosts)]
    for host in hosts:
        addLink(switch, host)