Пример #1
0
def pac_context_for_url(url, proxy_auth=None):
    """
    This context manager provides a simple way to add rudimentary PAC functionality
    to code that cannot be modified to use :class:`PACSession`,
    but obeys the ``HTTP_PROXY`` and ``HTTPS_PROXY`` environment variables.

    Upon entering this context, PAC discovery occurs with default parameters.
    If a PAC is found, then it's asked for the proxy to use for the given URL.
    The proxy environment variables are then set accordingly.

    Note that this provides a very simplified PAC experience that's insufficient for some scenarios.

    :param url: Consult the PAC for the proxy to use for this URL.
    :param requests.auth.HTTPProxyAuth proxy_auth: Username and password proxy authentication.
    """
    prev_http_proxy, prev_https_proxy = os.environ.get(
        'HTTP_PROXY'), os.environ.get('HTTPS_PROXY')
    pac = get_pac()
    if pac:
        resolver = ProxyResolver(pac, proxy_auth=proxy_auth)
        proxies = resolver.get_proxy_for_requests(url)
        # Cannot set None for environ. (#27)
        os.environ['HTTP_PROXY'] = proxies.get('http') or ''
        os.environ['HTTPS_PROXY'] = proxies.get('https') or ''
    yield
    if prev_http_proxy:
        os.environ['HTTP_PROXY'] = prev_http_proxy
    elif 'HTTP_PROXY' in os.environ:
        del os.environ['HTTP_PROXY']
    if prev_https_proxy:
        os.environ['HTTPS_PROXY'] = prev_https_proxy
    elif 'HTTPS_PROXY' in os.environ:
        del os.environ['HTTPS_PROXY']
Пример #2
0
def test_url_host_port_excluded():
    res = ProxyResolver(
        PACFile(
            'function FindProxyForURL(url, host) { return host.indexOf(" ") == -1 ? "PROXY PASS:80" : "PROXY FAIL:80"; }'
        ))
    for url in ('http://foo/bar', 'http://foo:80/bar'):
        assert res.get_proxy(url) == 'http://PASS:80'
Пример #3
0
 def __init__(self,
              pac_url: str = None,
              js: str = None,
              **kwargs: Any) -> None:
     super().__init__(**kwargs)
     if pac_url and "://" not in pac_url:
         pac_url = "http://" + pac_url
     self.pac_url = pac_url
     self._pac_file = get_pac(
         url=pac_url,
         js=js,
         allowed_content_types=[
             "application/octet-stream",
             "application/x-ns-proxy-autoconfig",
             "application/x-javascript-config",
         ],
     )
     self._resolver = ProxyResolver(self._pac_file)
Пример #4
0
    def __init__(self, *, url: str = "", pac_url: str = "") -> None:
        args: Dict[str, Any] = {}

        if pac_url:
            # Load the PAC file as PyPAC won't do it for us
            if pac_url.startswith("file:"):
                with open(pac_url.replace("file://", "")) as pac:
                    args["js"] = pac.read()
            else:
                args["url"] = pac_url
                args["allowed_content_types"] = [
                    "application/octet-stream",
                    "application/x-ns-proxy-autoconfig",
                    "application/x-javascript-config",
                ]

        self.pac_url = pac_url
        self._pac_file = get_pac(**args)
        self._resolver = ProxyResolver(self._pac_file)
Пример #5
0
 def _get_proxy_resolver(self, pac):
     return ProxyResolver(pac,
                          proxy_auth=self._proxy_auth,
                          socks_scheme=self._socks_scheme)
Пример #6
0
def _get_resolver(js_func_return_value, proxy_auth=None):
    """Get a ProxyResolver for a PAC that always returns the given value."""
    pac = PACFile('function FindProxyForURL(url, host) { return "%s"; }' % js_func_return_value)
    return ProxyResolver(pac, proxy_auth=proxy_auth)