python实现windows ie代理切换
生活随笔
收集整理的這篇文章主要介紹了
python实现windows ie代理切换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以前在杭州公司是需要設代理才可以連接外網,忽然一天不需要了,就沒怎么想過這個問題。
這次來無錫出差,天天需要設置代理,如果不...就會這樣:
回宿舍還得把代理取消。
遂偷懶...考慮了下...是否可以寫個程序運行完成...
首先查詢到,windows的ie代理實際上是修改注冊表項的相關內容參數完成的。
這下就簡單多了,只需修改注冊表里的項內容就行。
import io, sys, time, re, os import winreg #表項路徑 xpath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings"#設定代理,enable:是否開啟,proxyIp:代理服務器ip及端口,IgnoreIp:忽略代理的ip或網址 def setProxy(enable,proxyIp,IgnoreIp):try:key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, xpath, 0, winreg.KEY_WRITE)winreg.SetValueEx(key, "ProxyEnable", 0, winreg.REG_DWORD, enable)winreg.SetValueEx(key, "ProxyServer", 0, winreg.REG_SZ, proxyIp)winreg.SetValueEx(key, "ProxyOverride", 0, winreg.REG_SZ, IgnoreIp)except Exception as e:print("ERROR: " + str(e.args))finally:None#開啟,定義代理服務器ip及端口,忽略ip內容(分號分割) def enableProxy():proxyIP = "172.21.18.21:8080"IgnoreIp = "172.*;192.*;"print(" Setting proxy")setProxy(1,proxyIP,IgnoreIp)print(" Setting success")#關閉清空代理 def disableProxy():print(" Empty proxy")setProxy(0,"","")print(" Empty success")def main():place = input("where are you?(home or ls)\n")try:if place=="home":disableProxy()elif place=="ls":enableProxy()else:print("please input 'home' or 'ls'(longshine)!")main()except Exception as e:print("ERROR: " + str(e.args))finally:passif __name__ == '__main__':main()完成...其實后來想想用批處理也可以解決...反正就是修改注冊表項。
@echo off title 設置代理服務器 echo 正在設置代理服務器…… reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /d "172.21.18.21:8080" /f reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyOverride /t REG_SZ /d "172.*;192.*;" /f echo 設置完畢 @echo off title 清空代理服務器 沒有代理 echo 正在清空代理服務器設置…… reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /d "" /f echo 代理服務器設置已經清空總結
以上是生活随笔為你收集整理的python实现windows ie代理切换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FW:平凡(trivial)和非平凡(n
- 下一篇: python中哪些是无序_关于无序集合: