12

I have written a program that relies on a proxy to function. I now need a script that will check to see if the browser is set to use the right proxy, and if not, change it to use it. I need this implemented for as many browsers as possible, but is only required for Internet Explorer, Google Chrome, Mozilla Firefox, Safari and Opera. I am not even sure how to go about this, but it is for a project for work that will be due in a few days. If anyone can help or lend advice, I would greatly appreciate it!

I am programming on:
MS Windows XP
Python 2.6

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Zac Brown
  • 5,905
  • 19
  • 59
  • 107
  • 1
    Discussion on *reading* proxy settings here that you might be interested in: http://old.nabble.com/using-windows-wide-proxy-settings-td20822378.html I'm not sure I'd go around automatically changing users' proxy settings though (unless it's meant to be some kind of configuration shortcut?) Also, there's http://stackoverflow.com/questions/1201771/how-to-set-proxy-with-python – anton.burger Jun 16 '10 at 08:07
  • Yeah, I had seen those. I am looking for some example code or something. Thanks though. – Zac Brown Jun 19 '10 at 03:25
  • Would the pythonwin extension be able to help with this? – Zac Brown Jun 20 '10 at 01:08

3 Answers3

22

The Windows stores its system wide proxy in the registry, look in the the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings. You can use the Python _winreg module to change it (or just winreg if you use Python 3). Here is a sample code

import _winreg as winreg

INTERNET_SETTINGS = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
    r'Software\Microsoft\Windows\CurrentVersion\Internet Settings',
    0, winreg.KEY_ALL_ACCESS)

def set_key(name, value):
    _, reg_type = winreg.QueryValueEx(INTERNET_SETTINGS, name)
    winreg.SetValueEx(INTERNET_SETTINGS, name, 0, reg_type, value)

set_key('ProxyEnable', 1)
set_key('ProxyOverride', u'*.local;<local>')  # Bypass the proxy for localhost
set_key('ProxyServer', u'X.X.X.X:8080')

To disable it you can just need to set ProxyEnable key to 0:

set_key('ProxyEnable', 0)

After the script runs the browsers will still have the old proxy stored in-memory, so you need to restart them so they can re-read the new proxy settings from the registry. I found this to be very annoying so I converted this snippet to Python.

import ctypes

INTERNET_OPTION_REFRESH = 37
INTERNET_OPTION_SETTINGS_CHANGED = 39

internet_set_option = ctypes.windll.Wininet.InternetSetOptionW

internet_set_option(0, self.INTERNET_OPTION_REFRESH, 0, 0)
internet_set_option(0, self.INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)

This refreshes the Windows settings so all you have to do is hit F5 in your browser in order to it grab the new proxy settings.

I've written a small script to switch my proxy on or off, the source in on Bitbucket: https://bitbucket.org/canassa/switch-proxy

This should work with any browser that uses the Windows system-wide proxy (e.g.: Chrome, IE). Some browsers like Firefox uses a internal proxy settings. If you want to change these you will have figure out where they store their settings and write code to change it.

Community
  • 1
  • 1
Cesar Canassa
  • 18,659
  • 11
  • 66
  • 69
0

Much thanks to @Cesar Canassa, but there is a small problem, you should always refresh after you change the setting, otherwise the changes you just made in winreg will be discarded when you refresh. So it should be like this.

import ctypes

INTERNET_OPTION_REFRESH = 37
INTERNET_OPTION_SETTINGS_CHANGED = 39

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)
Dirk Paul
  • 129
  • 13
0

On or Off - IE Proxy (for Python 3.9):

import winreg
import ctypes

# On or Off - IE Proxy   (On=1 or Off=0)
proxyOnOff = 0

# Modification
INTERNET_SETTINGS = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
    r'Software\Microsoft\Windows\CurrentVersion\Internet Settings',
    0, winreg.KEY_ALL_ACCESS)
_, reg_type = winreg.QueryValueEx(INTERNET_SETTINGS, 'ProxyEnable')
winreg.SetValueEx(INTERNET_SETTINGS, 'ProxyEnable', 0, reg_type, proxyOnOff)

# Implementation
INTERNET_OPTION_REFRESH = 37
INTERNET_OPTION_SETTINGS_CHANGED = 39
internet_set_option = ctypes.windll.Wininet.InternetSetOptionW
internet_set_option(0, INTERNET_OPTION_REFRESH, 0, 0)
internet_set_option(0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)

# Read state - ProxyEnable
proxyEnable = winreg.QueryValueEx(INTERNET_SETTINGS, 'ProxyEnable')[0]
print(f"proxyEnable = {proxyEnable}")

input("Press Enter to continue...")
Leo S
  • 11
  • 2