python修改widows系统代理,爬虫

2021/6/18 9:27:20

本文主要是介绍python修改widows系统代理,爬虫,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import winreg


def edit_system_proxy(open_or_close, host, port):
    """
    修改系统代理函数
    :param open_or_close: 是否开启 bool
    :param host: IP
    :param port: 端口
    :return:
    """
    proxy = f"{host}:{port}"
    root = winreg.HKEY_CURRENT_USER
    proxy_path = r"Software\Microsoft\Windows\CurrentVersion\Internet Settings"
    kv_Enable = [
        (proxy_path, "ProxyEnable", 1, winreg.REG_DWORD),
        (proxy_path, "ProxyServer", proxy, winreg.REG_SZ),
    ]

    kv_Disable = [
        (proxy_path, "ProxyEnable", 0, winreg.REG_DWORD),
        # (proxy_path, "ProxyServer", proxy, winreg.REG_SZ),
    ]
    if open_or_close:
        kv = kv_Enable
    else:
        kv = kv_Disable
    for keypath, value_name, value, value_type in kv:
        hKey = winreg.CreateKey(root, keypath)
        winreg.SetValueEx(hKey, value_name, 0, value_type, value)


if __name__ == '__main__':
    edit_system_proxy(False, "127.0.0.1", "8848")

 



这篇关于python修改widows系统代理,爬虫的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程