需要定义名为“受攻击的\u开关”的全局变量

2024-06-01 13:15:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用GitHub的代码,通过泛光灯控制器中的反应流规则来缓解DDoS攻击。我无法运行该程序,因为没有定义全局变量attacked_switches,但我不知道如何执行该操作。有人能帮我吗

From GitHub

From YouTube

Images

def start():

    try:

        number_of_switches, number_of_hosts = topology_info()
        switch_dpids = list()
        hosts = dict()
        switch_dpids = switch_info(number_of_switches, switch_dpids)
        hosts = host_info(number_of_hosts, hosts)
        dpid = switch_byte(number_of_switches, switch_dpids)
        print 'dpid= ', dpid
        if dpid != 0:
            print'calling flow pusher'
            global attack
            attack = True
            attacked_switches.add(dpid)
            T2 = threading.Thread(target=Flowpusher, args=[dpid])
            T2.start()
        elif attack:
            T3 = threading.Thread(target=Flowremover, args=[dpid])
            T3.start()

    except Exception as e:
        print'Error occured:', e

Tags: offrominfogithubnumberstartprinthosts
1条回答
网友
1楼 · 发布于 2024-06-01 13:15:04

您只需在变量名之前(变量的第一个asignment之前)键入global即可定义全局变量。因此:

global attacked_switches

参见示例here

编辑:查看github代码,将attacked_switches指定为全局变量对您没有帮助。您引用的代码有attacked_switches.add(dpid)行,但我找不到attacked_switches的定义位置

相关问题 更多 >