carsonyl / pypac

Find and use proxy auto-config (PAC) files with Python and Requests.
https://pypac.readthedocs.io
Apache License 2.0
71 stars 19 forks source link

AttributeError: 'str' object has no attribute 'find_proxy_for_url' #1

Closed atronchi closed 7 years ago

atronchi commented 7 years ago

Running v0.1.0 downloaded from PyPI:

In [6]: session = PACSession('http://proxy.dataeng.mycompany.net/user/dataeng/proxy.pac')
In [7]: session.get("http://ip-100-74-44-105.ec2.internal:20888/proxy/application_1478638756790_1206426/")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-586997e4bdf7> in <module>()
----> 1 session.get("http://ip-100-74-44-105.ec2.internal:20888/proxy/application_1478638756790_1206426/")

/usr/local/lib/python2.7/site-packages/requests/sessions.pyc in get(self, url, **kwargs)
    485
    486         kwargs.setdefault('allow_redirects', True)
--> 487         return self.request('GET', url, **kwargs)
    488
    489     def options(self, url, **kwargs):

/usr/local/lib/python2.7/site-packages/pypac/api.pyc in request(self, method, url, proxies, **kwargs)
    155
    156         if using_pac:
--> 157             proxies = self._proxy_resolver.get_proxy_for_requests(url)
    158
    159         while True:

/usr/local/lib/python2.7/site-packages/pypac/resolver.pyc in get_proxy_for_requests(self, url)
     72             and 'DIRECT' is not configured as a fallback.
     73         """
---> 74         proxy = self.get_proxy(url)
     75         if not proxy:
     76             raise ProxyConfigExhaustedError(url)

/usr/local/lib/python2.7/site-packages/pypac/resolver.pyc in get_proxy(self, url)
     57         :rtype: str|None
     58         """
---> 59         proxies = self.get_proxies(url)
     60         for proxy in proxies:
     61             if proxy == 'DIRECT' or proxy not in self._offline_proxies:

/usr/local/lib/python2.7/site-packages/pypac/resolver.pyc in get_proxies(self, url)
     35         :rtype: list[str]
     36         """
---> 37         value_from_js_func = self.pac.find_proxy_for_url(url, urlparse(url).netloc)
     38         if value_from_js_func in self._cache:
     39             return self._cache[value_from_js_func]

AttributeError: 'str' object has no attribute 'find_proxy_for_url'

For reference, the PAC file contains:

function regExpMatch(url, pattern) {
    try { return new RegExp(pattern,"i").test(url); } catch(ex) { return false; }
}

function FindProxyForURL(url, host) {
  if (regExpMatch(host, "^h2td\.master\.dataeng\.mycompany\.net$") ||
      regExpMatch(host, "^bdp_h2td_[^\.]+\.master\.dataeng\.mycompany\.net$") ||
      regExpMatch(host, "^100\.74\.([0-9]|[0-9][0-9]|1[01][0-9]|12[0-7])\.[0-9]+$") ||
      regExpMatch(host, "^ip-100-74-([0-9]|[0-9][0-9]|1[01][0-9]|12[0-7])-[0-9]+(\.ec2\.internal)?$")
     ) {
    return "SOCKS5 proxy.dataeng.mycompany.net:7778";
  }

  if (regExpMatch(host, "^10\.20[0-2]\.[0-9]+\.[0-9]+$") ||
      regExpMatch(host, "^ip-10-20[0-2]-[0-9]+-[0-9]+(\.ec2\.internal)?$") ||
      regExpMatch(host, "^100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\.[0-9]+\.[0-9]+$") ||
      regExpMatch(host, "^ip-100-(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])-[0-9]+-[0-9]+(\.ec2\.internal)?$")
     ) {
    return "DIRECT";
  }

  if (regExpMatch(host, "^.*\.master\.dataeng\.mycompany\.net$") ||
      regExpMatch(host, "^.*\.internal$") ||
      regExpMatch(host, "^10\.[0-9]+\.[0-9]+\.[0-9]+$") ||
      regExpMatch(host, "^ip-10-[0-9]+-[0-9]+-[0-9]+$")
     ) {
    return "SOCKS5 proxy.dataeng.mycompany.net:7777";
  }

  return "DIRECT";
}
carsonyl commented 7 years ago

The first argument of PACSession, if provided, isn't the path to a PAC file (though I think that's a good idea). Instead, it needs to be a PACFile object. Try this instead: PACSession(get_pac('http://proxy.dataeng.mycompany.net/user/dataeng/proxy.pac')).

carsonyl commented 7 years ago

@atronchi Did you ever get this working?

carsonyl commented 7 years ago

I've expanded the readme to address this use case.

atronchi commented 7 years ago

Thanks! I ended up using js2py to directly load the pac file and run the functions