【问题标题】:How to disable network connection in Python Code如何在 Python 代码中禁用网络连接
【发布时间】:2014-08-28 17:22:32
【问题描述】:

是否可以在 Windows 7 中的 Python 中禁用和启用网络连接?我在这里看到了一个关于此的先前问题:How to programmatically enable/disable network interfaces? (Windows XP),但仍然找不到解决方案!请与我分享代码!马丁的回答给了我这个:b'Index Name

 \r\r\n0      WAN Miniport (SSTP)                                                   \r\r\n1      WAN Miniport (IKEv2)                                                  \r\r\n2      WAN Miniport (L2TP)                                                   \r\r\n3      WAN Miniport (PPTP)                                                   \r\r\n4      WAN Miniport (PPPOE)                                                  \r\r\n5      WAN Miniport (IPv6)                                                   \r\r\n6      WAN Miniport (Network Monitor)                                        \r\r\n7      Realtek RTL8102E/RTL8103E Family PCI-E Fast Ethernet NIC (NDIS 6.20)  \r\r\n8      WAN Miniport (IP)                                                     \r\r\n9      Microsoft ISATAP Adapter                                              \r\r\n10     RAS Async Adapter                                                     \r\r\n11     Atheros AR5007 802.11b/g WiFi Adapter                                 \r\r\n12     Teredo Tunneling Pseudo-Interface                                     \r\r\n13     Microsoft ISATAP Adapter #2                                           \r\r\n\r\r\n'

【问题讨论】:

    标签: python windows-7 python-3.3


    【解决方案1】:

    拍摄from here

    您需要使用subprocess 来启动以下命令行实用程序:

    启动提升的命令提示符。

    # Get NIC list and index number:
    wmic nic get name, index
    
    # Enable NIC with index number: (eg: 7)
    wmic path win32_networkadapter where index=7 call enable
    
    # Disable NIC with index number: (eg: 7)
    wmic path win32_networkadapter where index=7 call disable
    

    所以在 Python 中你会使用类似的东西

    import subprocess
    # get list of adapters and find index of adapter you want to disable.
    subprocess.check_output('wmic nic get name, index')
    

    要获取适配器列表,然后在知道索引后再次运行 subprocess.check_output 以获取其他命令。还要确保您以特权用户身份运行您的 python 脚本。

    【讨论】:

    • 更新了我的问题。这如何用于禁用网络连接?
    • 确保strip 输出:subprocess.check_output('wmic nic get name, index').strip() - 这将删除\n\n 字符。然后你的第一个字符将是0,这是索引。然后您应该可以致电wmic path win32_networkadapter where index=0 call disable
    • 出于某种原因,.strip() 没有删除 '\r' 字符!
    • 所以我需要在所有这些中找到网络适配器?怎么样?
    猜你喜欢
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 2020-01-11
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多