0

我有一系列不时更改的 IP,我希望每个出现的新 IP 都可以运行命令。

我的代码是:

while (network.status!="connected"):
    p=network.connections  
    for i in p:
        print i.ip  #checks the IP's in the array i
    time.sleep(10)    

所以我想每当数组中有一个新值时我就运行一个特定的命令。在 python 中执行此操作的最有效方法是什么。

4

2 回答 2

5

使用 aset并查看每个循环中的差异:

old = set()
while network.status != "connected":
    p = set(network.connections)
    for i in p - old:
        print i.ip # new ips that were added
    for i in old - p:
        print i.ip # old ips that were removed
    old = p
    time.sleep(10)   
于 2012-11-12T13:47:58.757 回答
0

将列表子类化并将其连接到某个处理程序怎么样?看看这里: https ://stackoverflow.com/a/12203829/1091116 您想要做的是重命名“验证”并使其对添加列表中的项目做出反应。

于 2012-11-12T13:57:40.170 回答