8

How do I include my automatic proxy config file in HTTP libraries like urllib or requests.

pacfile = 'http://myintranet.com/proxies/ourproxies.pac'
proxy = urllib3.ProxyManager(????????????????)
Kaymatrix
  • 615
  • 2
  • 8
  • 16

3 Answers3

12

I've created a pure-Python library called PyPAC which should do what you're looking for. It provides a subclass of requests.Session that includes honours PACs and includes PAC auto-discovery.

Carson Lam
  • 313
  • 3
  • 9
  • 1
    How is the best pratice to use pypac in requests? – alfi Oct 12 '17 at 02:41
  • i'm tinkering with monkey patching urlllib. it's the only legit way to cover everything, since you may be using a library that you can't modify, which, in turn, uses requests or urllib or both. – Erik Aronesty Jan 13 '20 at 17:06
  • @ErikAronesty [pypac.pac_context_for_url()](https://pypac.readthedocs.io/en/latest/api.html#pypac.pac_context_for_url) is a bit of a cheat way to get libraries working under a PAC network, as long as those libraries recognize proxy environment variables. It's not perfect, but it'll get people over the PAC hurdle in most cases. – Carson Lam Jan 14 '20 at 18:23
11

Current there is no support for a proxy PAC file directly in urllib3 or requests. While support could in principle be added for proxy PAC files, because they are Javascript files that require interpretation it is likely to be extremely difficult to provide broad-based support.

In principle you could use requests/urllib3 to request the Proxy PAC file, then pass it to something like Node.JS for interpreting, then parse the results back in Python to pass to urllib3/requests, but nothing like that exists out of the box.

Lukasa
  • 14,599
  • 4
  • 32
  • 34
4

Use PYPAC.

from pypac import PACSession, get_pac

pac = get_pac(url='http://your/pac/url/file.pac')
session = PACSession(pac, proxy_auth=HTTPProxyAuth('your_user', 'password'))
print(session.get('http://www.google.com'))

you will get a 200

Marcelo Guedes
  • 1,419
  • 11
  • 10