see code.I want to open the proxy and then close the proxy. in fact, i find it is normal to turn on proxy and turn off proxy, but they cannot be used at the same time. I haven't found a solution yet. Please help me. Thank you
import ctypes
import winreg
import time
import requests
xpath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings"
internet_set_option = ctypes.windll.Wininet.InternetSetOptionW
def setProxy(enable, proxy):
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, proxy)
# 关闭自动配置脚本
winreg.SetValueEx(key, "AutoConfigURL", 0, winreg.REG_SZ, "")
# 设置刷新
INTERNET_OPTION_REFRESH = 37
INTERNET_OPTION_SETTINGS_CHANGED = 39
internet_set_option(0, INTERNET_OPTION_REFRESH, 0, 0)
internet_set_option(0,INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
except Exception as e:
print("ERROR: " + str(e))
finally:
None
def enableProxy(proxy):
print(" Setting proxy")
setProxy(1, proxy)
print(" Setting success")
#关闭清空代理
def disableProxy():
print(" Empty proxy")
setProxy(0, "")
print(" Empty success")
if __name__ == '__main__':
enableProxy('42.51.13.68:16819')
print(requests.get("http://dev.kdlapi.com/testproxy").text)
time.sleep(1)
disableProxy()
print(requests.get("http://dev.kdlapi.com/testproxy").text)