Windows+Python代理检测

python requests的代理检测

def getproxies_environment():
        
    proxies = {}
    for name, value in os.environ.items():
        name = name.lower()
        if value and name[-6:] == '_proxy':
            proxies[name[:-6]] = value

    if 'REQUEST_METHOD' in os.environ:
        proxies.pop('http', None)

    for name, value in os.environ.items():
        if name[-6:] == '_proxy':
            name = name.lower()
            if value:
                proxies[name[:-6]] = value
            else:
                proxies.pop(name[:-6], None)

    return proxies


def getproxies_registry(self):
    
    proxies = {}
    try:
        internetSettings = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
            r'Software\Microsoft\Windows\CurrentVersion\Internet Settings')
        proxyEnable = winreg.QueryValueEx(internetSettings,
                                        'ProxyEnable')[0]
        if proxyEnable:
            # Returned as Unicode but problems if not converted to ASCII
            proxyServer = str(winreg.QueryValueEx(internetSettings,
                                                'ProxyServer')[0])
            if '=' in proxyServer:
                # Per-protocol settings
                for p in proxyServer.split(';'):
                    protocol, address = p.split('=', 1)
                    # See if address has a type:// prefix
                    if not re.match('^([^/:]+)://', address):
                        address = '%s://%s' % (protocol, address)
                    proxies[protocol] = address
            else:
                # Use one setting for all protocols
                if proxyServer[:5] == 'http:':
                    proxies['http'] = proxyServer
                else:
                    proxies['http'] = 'http://%s' % proxyServer
                    proxies['https'] = 'https://%s' % proxyServer
                    proxies['ftp'] = 'ftp://%s' % proxyServer
        internetSettings.Close()
    
    except (OSError, ValueError, TypeError):
        pass
    
    return proxies

PAC代理检测

需要用到一个第三方模块儿pypac,需要传入一个url,根据pac文件判断
该地址是否需要代理
pac文件内容:
在这里插入图片描述

def get_pac_proxy(server_url):
    from pypac import get_pac
    try:
        reg_path = r'Software\Microsoft\Windows\CurrentVersion\Internet Settings'
        ie_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, reg_path, 0, (winreg.KEY_WOW64_64KEY + winreg.KEY_READ))
        pac_url, _ = winreg.QueryValueEx(ie_key, "AutoConfigURL")
        pac = get_pac(url=pac_url)
        res = urlparse(server_url)
        proxy_address = pac.find_proxy_for_url(server_url, res.netloc)
        proxys = proxy_address.split(";")
        if proxys and len(proxys) > 0:
            proxy_address = proxys[0]
        proxy_address = proxy_address.replace("PROXY", "", -1)
        proxy_address = proxy_address.replace(";", "", -1)
        proxy_address = proxy_address.strip()
        items = proxy_address.split(":")
        if len(items) == 2:
            return items[0], items[1], "HTTP"
    except Exception as e:
        # traceback.print_exc()
        pass
    return "", "", ""

代理判断

  1. 可以通过powershell设置临时代理
    set http_proxy=http://127.0.0.1:1080
    set https_proxy=http://127.0.0.1:1080
    
  2. PAC代理
    在这里插入图片描述
  3. 全局代理
    在这里插入图片描述
    注册表中的信息也是有区别的:
    Win+R 输入regedit
    在这里插入图片描述

PAC代理:
在这里插入图片描述

全局代理:
在这里插入图片描述
以上针对加速器也是可以检测出来的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值