-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin_proxy_setting.py
More file actions
36 lines (27 loc) · 1.17 KB
/
Copy pathwin_proxy_setting.py
File metadata and controls
36 lines (27 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import ctypes
import winreg
INTERNET_OPTION_SETTINGS_CHANGED = 39
INTERNET_OPTION_REFRESH = 37
def set_proxy_config(port):
host = 'localhost:{0}'.format(str(port))
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings', 0, winreg.KEY_WRITE) as key:
winreg.SetValueEx(key, 'ProxyEnable', 0, winreg.REG_DWORD, 1)
winreg.SetValueEx(key, 'ProxyServer', 0, winreg.REG_SZ, host)
refresh()
except Exception as ex:
print(ex)
def back_proxy_config():
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings', 0, winreg.KEY_WRITE) as key:
winreg.SetValueEx(key, 'ProxyEnable', 0, winreg.REG_DWORD, 0)
winreg.SetValueEx(key, 'ProxyServer', 0, winreg.REG_SZ, '')
refresh()
except Exception as ex:
print(ex)
def refresh():
internet_set_option = ctypes.windll.wininet.InternetSetOptionW
internet_set_option(0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
internet_set_option(0, INTERNET_OPTION_REFRESH, 0, 0)
if __name__ == '__main__':
back_proxy_config()