在 For 循环中使用 Timer()
Using Timer() inside For loop
我正在使用 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
我该如何解决这个问题?
操作%必须在元组内,也最好将任务分开,这样更易读。:
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()
我正在使用 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
我该如何解决这个问题?
操作%必须在元组内,也最好将任务分开,这样更易读。:
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()