python3操作注册表设置/取消IE代理

import io, sys, time, re, os
import winreg


def enableProxy(IP, Port):
    proxy = IP + ":" + str(Port)
    xpath = "SoftwareMicrosoftWindowsCurrentVersionInternet Settings"
    try:
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, xpath, 0, winreg.KEY_WRITE)
        winreg.SetValueEx(key, "ProxyEnable", 0, winreg.REG_DWORD, 1)
        winreg.SetValueEx(key, "ProxyServer", 0, winreg.REG_SZ, proxy)
    except Exception as e:
        print("ERROR: " + str(e.args))
    finally:
        None

def disableProxy():
    proxy = ""
    xpath = "SoftwareMicrosoftWindowsCurrentVersionInternet Settings"
    try:
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, xpath, 0, winreg.KEY_WRITE)
        winreg.SetValueEx(key, "ProxyEnable", 0, winreg.REG_DWORD, 0)
        winreg.SetValueEx(key, "ProxyServer", 0, winreg.REG_SZ, proxy)
    except Exception as e:
        print("ERROR: " + str(e.args))
    finally:
        None

def main():
    proxyIP = "10.10.1.2"
    proxyPort = 123

    try:
        disableProxy()
        enableProxy(proxyIP, proxyPort)
except Exception as e: print("ERROR: " + str(e.args)) finally: pass if __name__ == '__main__': main()
原文地址:https://www.cnblogs.com/lanzhi/p/6468518.html