如何在Python代码中禁用网络连接

时间:2014-08-28 17:22:33

标签: python windows-7 python-3.3

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

 \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'

1 个答案:

答案 0 :(得分:2)

采取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脚本。