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

时间:2017-12-26 17:20:40

标签: python network-programming mininet

#!/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脚本时遇到此错误。

1 个答案:

答案 0 :(得分:1)

看起来你对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"]