Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Proxy PAC file for python urllib or request?

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(????????????????)
like image 272
Kaymatrix Avatar asked Jul 21 '15 01:07

Kaymatrix


3 Answers

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.

like image 82
Carson Lam Avatar answered Sep 25 '22 08:09

Carson Lam


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.

like image 36
Lukasa Avatar answered Sep 24 '22 08:09

Lukasa


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

like image 38
Marcelo Guedes Avatar answered Sep 23 '22 08:09

Marcelo Guedes