**
**
平时经常要禁用网络测试一些东西,所以想搞个这个东西,节约一点时间
主要是利用windows指令:netsh interface set interface 以太网 disabled/enabled
import os import time # from __future__ import print_function import ctypes, sys,click def is_admin(): try: print('is_admin==true') return ctypes.windll.shell32.IsUserAnAdmin() except: print('is_admin==false') return False if is_admin(): # 将要运行的代码加到这里 print('disable') show_cmd = 'netsh interface show interface' # var = os.system(show_cmd) # print('vat='+var) disable_cmd = 'netsh interface set interface 以太网 disabled' os.system(disable_cmd) while True: click.secho('disabled',fg='green') click.secho('press any key to enable network',fg='red') click.getchar() disable_cmd = 'netsh interface set interface 以太网 enabled' os.system(disable_cmd) break else: if sys.version_info[0] == 3: ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1) else:#in python2.x ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable), unicode(__file__), None, 1)
禁用网络后可以按任意按键恢复: