Peter
Peter

Reputation: 23

Python change global proxy settings

I am wondering if anyone knows how to change the global proxy settings of a windows 10 machine, through python. My goal is it to create a script which requests proxies and then sets them global for the entire machine. So if I open chrome or any Browser the traffic flows through the proxy.

But this technique only effects traffic, which is running through cmd

  1. I tried for Example the netsh function in cmd:

    netsh set proxy ..
    
  2. I also tried starting the chrome browser with the proxy, but that also didn't work, referring to this article: https://winaero.com/blog/override-proxy-settings-google-chrome/

My code until now is only grabbing the proxy from a website with list of proxies.

Thanks

Upvotes: 2

Views: 1338

Answers (1)

Pedro Lobito
Pedro Lobito

Reputation: 98901

To programmatically set a system wide proxy on windows, you can change the registry, i.e.:

from winreg import *

proxy = "127.0.0.1:8080"
status = 1 # 0 disable 1 enable

keyVal = 'Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
key = OpenKey(HKEY_CURRENT_USER, keyVal, 0, KEY_ALL_ACCESS)
SetValueEx(key, "ProxyServer", 0, REG_SZ, proxy)
SetValueEx(key, "ProxyEnable", 0, REG_DWORD, status)
CloseKey(key)

Notes:

In my case, I had to close and re-open Firefox for it to acknowledge the new proxy settings.

Upvotes: 1

Related Questions