无线连接与代理

一、代理

1、说明

在办公网络中经常需要设置,现在准备使用python操作代理

打开代理,输入内容

关闭代理

2、代码

import winreg


class LocalAgent:
    def __init__(self):
        self.KEY_ProxyEnable = "ProxyEnable"
        self.KEY_ProxyServer = "ProxyServer"
        self.KEY_ProxyOverride = "ProxyOverride"
        self.KEY_XPATH = "Software\Microsoft\Windows\CurrentVersion\Internet Settings"
        self.proxyIp = "xxx:8080"   # ip地址
        self.IgnoreIp = "XXXX"   # 忽略ip

    def SetProxy(self,enable, proxyIp, IgnoreIp):
        hKey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, self.KEY_XPATH, 0, winreg.KEY_WRITE)
        winreg.SetValueEx(hKey, self.KEY_ProxyEnable, 0, winreg.REG_DWORD, enable)
        winreg.SetValueEx(hKey, self.KEY_ProxyServer, 0, winreg.REG_SZ, proxyIp)
        winreg.SetValueEx(hKey, self.KEY_ProxyOverride, 0, winreg.REG_SZ, IgnoreIp)
        winreg.CloseKey(hKey)

    # 获取当前代理状态
    def GetProxyStatus(self):
        hKey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, self.KEY_XPATH, 0, winreg.KEY_READ)
        retVal = winreg.QueryValueEx(hKey, self.KEY_ProxyEnable)
        winreg.CloseKey(hKey)
        return retVal[0] == 1

    def close_agent(self):
        print('正在进行关闭本机代理操作。。。')
        if self.GetProxyStatus():
            self.SetProxy(0, "", "")
            print("成功关闭代理!!!")
        else:
            print('代理是关闭状态,无需操作!!!')

    def open_agent(self):
        print('正在进行打开本机代理操作。。。')
        if self.GetProxyStatus():
            print('代理是打开状态,无需操作!!!')
        else:
            self.SetProxy(1,self.proxyIp,self.IgnoreIp)
            print('成功打开代理!!!')


if __name__ == '__main__':
    agent = LocalAgent()
    agent.close_agent()
    # agent.open_agent()

3、展示

打开代理

 

 

关闭代理

 

 二、无线网

使用python连接指定的无线网

1、代码

import pywifi
from pywifi import const


class LocalWifi:
    def __init__(self):
        wifi = pywifi.PyWiFi()
        self.iface = wifi.interfaces()[0]
        self.profile = pywifi.Profile()
        # self.profile.ssid = 'ES-003471'
        self.profile.ssid = 'CMCC-302'   # wifi名
        self.profile.auth = const.AUTH_ALG_OPEN
        self.profile.akm.append(const.AKM_TYPE_WPA2PSK)
        self.profile.cipher = const.CIPHER_TYPE_CCMP
        # self.profile.key = 'xiaotian'
        self.profile.key = '13366208366'   # wifi密码

    def close_wifi(self):
        print('正在关闭本机wifi。。。')
        self.iface.disconnect()
        print('关闭wifi成功!!!')

    def open_wifi(self):
        print('正在打开本机wifi。。。')
        self.iface.remove_all_network_profiles()
        tem_profile = self.iface.add_network_profile(self.profile)
        self.iface.connect(tem_profile)
        print('打开本机wifi成功!!!')


if __name__ == '__main__':
    mywifi = LocalWifi()
    # mywifi.close_wifi()
    mywifi.open_wifi()

2、说明

修改wifi名和密码就可以使用,不展示了,现在使用的是办公环境

3、wifi、代理、本地网络,组合起来,就是你平时的办公环境和非办公环境,上面的代码调用,再写一个bat脚本,可以一键切换办公网络和非办公网络。

亲测可用。

posted @ 2020-02-28 11:51  tianxiaodema  阅读(294)  评论(0编辑  收藏  举报