简体   繁体   中英

How to disable network connection in Python Code

Is it possible to disable and enable network connection in Python in Windows 7? I saw a previous question about this here: How to programmatically enable/disable network interfaces? (Windows XP) , but still couldn't find a solution! Please share the code with me! Martin's answer gave me this: 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'

Taken from here :

You need to use subprocess to start the following command-line utilities:

Start elevated Command Prompt.

# 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

So in Python you would use something like

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

To get the list of adapters, then run subprocess.check_output again for the other commands once you know the index. Also make sure you are running your python script as a privileged user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM