Luk*_*ler 1 python windows-7 python-3.3
是否可以在 Windows 7 中禁用和启用 Python 中的网络连接?我在这里看到了一个以前的问题:如何以编程方式启用/禁用网络接口?(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'
Run Code Online (Sandbox Code Playgroud)
采取从这里:
您需要使用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
Run Code Online (Sandbox Code Playgroud)
所以在 Python 中你会使用类似的东西
import subprocess
# get list of adapters and find index of adapter you want to disable.
subprocess.check_output('wmic nic get name, index')
Run Code Online (Sandbox Code Playgroud)
要获取适配器列表,subprocess.check_output请在知道索引后再次运行其他命令。还要确保您以特权用户身份运行您的 python 脚本。