代码语言:javascript复制
import socket import os import time import winreg import wmi '''''set/unset proxy, according to ip addr''' def proxy(enable): root = winreg.HKEY_CURRENT_USER subkey = r'SoftwareMicrosoftWindowsCurrentVersionInternet Settings' valuename = 'ProxyEnable' key = winreg.OpenKey(root, subkey, access=winreg.KEY_ALL_ACCESS) value, t = winreg.QueryValueEx(key, valuename) #print(value, t) if enable: if value == 1: print('Already enable proxy!') return 0 winreg.SetValueEx(key, valuename, 0, t, 1) killie() print('Enable proxy Done!') else: if value == 0: print('Already unable proxy!') return 0 winreg.SetValueEx(key, valuename, 0, t, 0) killie() print('Disable proxy Done') def unproxy(): proxy(False) def killie(): ''' if ie running, kill it. make change takes effect''' c = wmi.WMI(find_classes=False) # turn off introspection for p in c.Win32_Process(['Name']): if p.Name == 'iexplore.exe': os.system('taskkill /im iexplore.exe /f') return print('Not found iexplore.exe!') def getipaddr(): '''first get all interface addr''' #hostname = socket.getfqdn(socket.gethostname()) hostname = socket.gethostname() hostaliasip = socket.gethostbyname_ex(hostname) #host, hostalias, iplist = hostaliasip return hostaliasip def main(iplist): '''''check whether at room, if true, cancel proxy''' network = '192.168.1.' for ip in iplist: if network in ip:
unproxy() return 0
# else enable proxy
proxy(True) if __name__ == '__main__': main(getipaddr()[2]) time.sleep(2)