不可用类型:在mininet中运行python脚本时出现'list'错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不可用类型:在mininet中运行python脚本时出现'list'错误相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import Link
from mininet.log import setLogLevel, info
from mininet.cli import CLI

class myNetwork(Topo):
 "three users connected to cloud"

 def build(self, **_opts):
  #create switch
  s1 = 
  [ self.addSwitch for s in 's1' ]
  #create hosts
  h1, h2, h3 = 
  [ self.addHosts for h in 'h1', 'h2', 'h3' ]
  #create links
  [for h,s in [(h1,s1), (h2,s1), (h3,s1)]: self.addLink(h,s)

def run():  
 topo = myNetwork()
 net = Mininet(topo=topo)
 net.start()
 CLI(net)
 net.stop()

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

当我尝试在mininet中运行我的python脚本时,我收到错误:

不可用类型:'列表'

我试图研究这个,我理解错误意味着什么,但我不确定为什么我运行我的python脚本时出现这个错误。

答案

看起来你对List Comprehensions的工作方式有点困惑。你可能想要做

[ self.addSwitch for s in s1 ] 
# notice the lack of quotes around s1, this means we want the VAR not the string

代替

[ self.addSwitch for s in 's1' ]

此外,您可能忘记从此行中删除[,因为我认为这不是正确的语法:

[for h,s in [(h1,s1), (h2,s1), (h3,s1)]: self.addLink(h,s)

至:

for h,s in [(h1,s1), (h2,s1), (h3,s1)]: self.addLink(h,s)

以下是列表理解的几个示例:

l = [1, 2, 3, 4]

l_modified = [i+1 for i in l] # [2, 3, 4, 5]

a = "String"
a_list = [c for c in a] # ["S", "t", "r", "i", "n", "g"]

b = [c.upper() for c in "hello"] # ["H", "E", "L", "L", "O"]

以上是关于不可用类型:在mininet中运行python脚本时出现'list'错误的主要内容,如果未能解决你的问题,请参考以下文章

3 Mininet命令延伸实验拓展

mininet之miniedit可视化操作

ubuntu中安装好了mininet 但是运行python代码时,报错?

4 Mininet测量路径的损耗率

如何运行mininet2.2.1中的图形界面

SDN的Mininet使用