2014-08-28 63 views
-1

是否可以在Windows 7中禁用和啓用Python中的網絡連接?我在這裏看到了一個關於此的前一個問題:How to programmatically enable/disable network interfaces? (Windows XP),但仍找不到解決方案!請與我分享代碼!馬丁的回答給了我這個:b'Index名稱如何在Python中禁用網絡連接代碼

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

回答

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腳本。

+0

更新了我的問題。這是如何禁用網絡連接的? – 2014-08-28 18:48:38

+0

確保你'輸出':'subprocess.check_output('wmic nic get name,index')。strip()' - 這將刪除'\ n \ n'字符。那麼你的第一個字符就是'0',這是索引。您應該可以調用'wmic path win32_networkadapter where index = 0 call disable' – 2014-08-28 18:52:00

+0

由於某些原因,.strip()不會刪除'\ r'字符! – 2014-08-28 19:01:26