Programmatically detect system-proxy settings on Windows XP with Python

前端 未结 2 1045
遥遥无期
遥遥无期 2021-01-01 04:32

I develop a critical application used by a multi-national company. Users in offices all around the globe need to be able to install this application.

The application

相关标签:
2条回答
  • 2021-01-01 04:51

    As far as I know, In a Windows environment, if no proxy environment variables are set, proxy settings are obtained from the registry's Internet Settings section. . Isn't it enough?

    Or u can get something useful info from registry: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer

    Edit:
    sorry for don't know how to format comment's source code, I repost it here.

    >>> import win32com.client
    >>> js = win32com.client.Dispatch('MSScriptControl.ScriptControl')
    >>> js.Language = 'JavaScript'
    >>> js.AddCode('function add(a, b) {return a+b;}')
    >>> js.Run('add', 1, 2)
    3
    
    0 讨论(0)
  • 2021-01-01 04:55

    Here's a sample that should create a bullet green (proxy enable) or red (proxy disable) in your systray

    It shows how to read and write in windows registry

    it uses gtk

    #!/usr/bin/env python
    import gobject
    import gtk
    from _winreg import *
    
    class ProxyNotifier:
        def __init__(self):        
            self.trayIcon = gtk.StatusIcon()
            self.updateIcon()
    
            #set callback on right click to on_right_click
            self.trayIcon.connect('popup-menu', self.on_right_click)
            gobject.timeout_add(1000, self.checkStatus)
    
        def isProxyEnabled(self):
    
            aReg = ConnectRegistry(None,HKEY_CURRENT_USER)
    
            aKey = OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings") 
            subCount, valueCount, lastModified = QueryInfoKey(aKey)
    
            for i in range(valueCount):                                           
                try:
                    n,v,t = EnumValue(aKey,i)
                    if n == 'ProxyEnable':
                        return v and True or False
                except EnvironmentError:                                               
                    break
            CloseKey(aKey)  
    
        def invertProxyEnableState(self):
            aReg = ConnectRegistry(None,HKEY_CURRENT_USER)
            aKey = OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", 0, KEY_WRITE)
            if self.isProxyEnabled() : 
                val = 0 
            else:
                val = 1
            try:   
                SetValueEx(aKey,"ProxyEnable",0, REG_DWORD, val) 
            except EnvironmentError:                                          
                print "Encountered problems writing into the Registry..."
            CloseKey(aKey)
    
        def updateIcon(self):
            if self.isProxyEnabled():
                icon=gtk.STOCK_YES
            else:
                icon=gtk.STOCK_NO
            self.trayIcon.set_from_stock(icon)
    
        def checkStatus(self):
            self.updateIcon()
            return True
    
    
        def on_right_click(self, data, event_button, event_time):
            self.invertProxyEnableState()
            self.updateIcon()
    
    
    if __name__ == '__main__':
        proxyNotifier = ProxyNotifier()
        gtk.main()
    
    0 讨论(0)
提交回复
热议问题