requests 开启charles代理报错

当前报错urllib3 版本: 1.26.7

解决代理报错的几种方式:

  • 配置代理请求
proxies = {
        'http': 'http://127.0.0.1:8888',
        'https': 'http://127.0.0.1:8888'
    }
data = requests.post(url, json=body, headers=headers, verify=False, proxies=proxies)
# 注意 https 后面值的协议也是http或者不要协议

  • 打补丁: address = '%s://%s' % (protocol, address) 改成了 address = '%s' % (address)
  def my_getproxies_registry():
    """Return a dictionary of scheme -> proxy server URL mappings.

    Win32 uses the registry to store proxies.

    """
    proxies = {}
    try:
        import winreg
    except ImportError:
        # Std module, so should be around - but you never know!
        return 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' % (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):
        # Either registry key not found etc, or the value in an
        # unexpected format.
        # proxies already set up to be empty so nothing to do
        pass
    return proxies
if os.name == 'nt':
    from urllib import request
    request.getproxies_registry = my_getproxies_registry
  • 降级urllib3: pip3 install urllib3==1.23

posted on 2024-06-13 16:43  游荡的鱼  阅读(13)  评论(0编辑  收藏  举报

导航