我正在使用mininet模拟器。我想每7秒向主机发送一次命令。我正在使用
threading.Timer()
在for循环中。我需要将参数作为变量传递给函数,该变量改变了循环中的每次迭代 像这样
for ip in ips:
t =threading.Timer(7, attacker.cmd ,args= ("sudo python SYN-ACK.py %s 192.168.1.100 %d 80",))%(ip,ips[ip])
然而,这给了我这个错误
t =threading.Timer(7, attacker.cmd ,args= ("sudo python SYN-ACK.py %s 192.168.1.100 %d 80",))%(ip,ips[ip])
TypeError: unsupported operand type(s) for %: '_Timer' and 'tuple'
尝试上述解决方案后,我得到了这个例外
Exception in thread Thread-31:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 1082, in run
self.function(*self.args, **self.kwargs)
File "build/bdist.linux-x86_64/egg/mininet/node.py", line 355, in cmd
self.sendCmd( *args, **kwargs )
File "build/bdist.linux-x86_64/egg/mininet/node.py", line 272, in
sendCmd
assert self.shell and not self.waiting
AssertionError
我怎么解决这个问题?
答案 0 :(得分:0)
操作%必须在元组内部,最好将任务分开以使其更具可读性。:
for ip, port in ips.items():
cmd = "sudo python SYN-ACK.py %s 192.168.1.100 %d 80" % (ip, port)
t =threading.Timer(7, some_print,args= (cmd,))
t.start()