i have written little script needs able enable , disable proxy settings python. right edit registry achieve this, doesn't seem work on versions of windows, rather use internetsetoption. information api scarce , of examples in c, don't know: https://support.microsoft.com/en-us/kb/226473
it (this snippet refreshing browser proxy settings):
import ctypes internet_option_refresh = 37 internet_option_settings_changed = 39 internet_set_option = ctypes.windll.wininet.internetsetoptionw internet_set_option(0, 38, 0, 0) internet_set_option(0, internet_option_refresh, 0, 0) internet_set_option(0, internet_option_settings_changed, 0, 0)
i figured out myself, through lots of trial , errors. working example:
from ctypes import * ctypes.wintypes import * lpwstr = pointer(wchar) hinternet = lpvoid internet_per_conn_proxy_server = 2 internet_option_refresh = 37 internet_option_settings_changed = 39 internet_option_per_connection_option = 75 internet_per_conn_proxy_bypass = 3 internet_per_conn_flags = 1 class internet_per_conn_option(structure): class value(union): _fields_ = [ ('dwvalue', dword), ('pszvalue', lpwstr), ('ftvalue', filetime), ] _fields_ = [ ('dwoption', dword), ('value', value), ] class internet_per_conn_option_list(structure): _fields_ = [ ('dwsize', dword), ('pszconnection', lpwstr), ('dwoptioncount', dword), ('dwoptionerror', dword), ('poptions', pointer(internet_per_conn_option)), ] def set_proxy_settings(ip, port, on=true): if on: setting = create_unicode_buffer(ip+":"+str(port)) else: setting = none internetsetoption = windll.wininet.internetsetoptionw internetsetoption.argtypes = [hinternet, dword, lpvoid, dword] internetsetoption.restype = bool list = internet_per_conn_option_list() option = (internet_per_conn_option * 3)() nsize = c_ulong(sizeof(internet_per_conn_option_list)) option[0].dwoption = internet_per_conn_flags option[0].value.dwvalue = (2 if on else 1) # proxy_type_direct or option[1].dwoption = internet_per_conn_proxy_server option[1].value.pszvalue = setting option[2].dwoption = internet_per_conn_proxy_bypass option[2].value.pszvalue = create_unicode_buffer("localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;172.32.*;192.168.*") list.dwsize = sizeof(internet_per_conn_option_list) list.pszconnection = none list.dwoptioncount = 3 list.dwoptionerror = 0 list.poptions = option internetsetoption(none, internet_option_per_connection_option, byref(list), nsize) internetsetoption(none, internet_option_settings_changed, none, 0) internetsetoption(none, internet_option_refresh, none, 0) set_proxy_settings("127.0.0.1", 52042)
Comments
Post a Comment