Which Python module to use to access the Proxy set

2019-05-10 09:07发布

I am new to Python and would like to write a script 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

3条回答
聊天终结者
2楼-- · 2019-05-10 09:28

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.

查看更多
聊天终结者
3楼-- · 2019-05-10 09:29

I'm having similar question too. At the moment, I just use os module and using Windows reg command to get the proxy setting from registry. Hope this helps.

>>> import os
>>> os.system('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" | findstr "ProxyServer AutoConfigURL"')
    ProxyServer    REG_SZ    http=127.0.0.1:8080;https=127.0.0.1:8080
    AutoConfigURL    REG_SZ    http://proxy/wpad.dat
>>>
查看更多
一夜七次
4楼-- · 2019-05-10 09:46

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
查看更多
登录 后发表回答