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

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


def enableProxy(IP, Port):
    proxy = IP + ":" + str(Port)
    xpath = "Software\Microsoft\Windows\CurrentVersion\Internet 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 = "Software\Microsoft\Windows\CurrentVersion\Internet 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()

 

posted @ 2012-06-15 18:16  方倍工作室  阅读(2003)  评论(0编辑  收藏  举报