python一键禁用网络

**

python一键禁用网络

**
平时经常要禁用网络测试一些东西,所以想搞个这个东西,节约一点时间
主要是利用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)


禁用网络后可以按任意按键恢复:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值