开发者

Which Python module to use to access the Proxy setting of Windows 7?

I am new to Python and would like to write a script 开发者_JAVA百科to change Windows proxy settings based on the network I am connected to. Is there any existing python module I can use? Appreciate your help.

Thanks, Sethu


I would use winreg and query the settings directly from the registry.

 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet
 Settings] "MigrateProxy"=dword:00000001 
 "ProxyEnable"=dword:00000001
 "ProxyHttp1.1"=dword:00000000
 "ProxyServer"="http://ProxyServername:80" 
 "ProxyOverride"="<local>"

For example, something like:

import _winreg

def getProxy():
    proxy = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")
    server, type = _winreg.QueryValueEx(proxy, "ProxyServer")
    enabled, type = _winreg.QueryValueEx(proxy, "ProxyEnable")
    if enabled:
        return server
    return None


Cannot you set the HTTP_PROXY environment variable in Windows (either manually or within your program) for your application before sending the request? That should take care that any request you send it via urllib2 goes via Proxy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜