以前在杭州公司是需要设代理才可以连接外网,忽然一天不需要了,就没怎么想过这个问题。
这次来无锡出差,天天需要设置代理,如果不...就会这样:
回宿舍还得把代理取消。
遂偷懒...考虑了下...是否可以写个程序运行完成...
首先查询到,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:
pass
if __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 代理服务器设置已经清空